问题
I have a csv file containing tuples
Test.csv
"(1,2,3)","(1,2,4)","(1,2,5)"
I want to import Test.csv as a list of tuples.
Expected Result
new_list = [(1,2,3),(1,2,4),(1,2,5)]
Code Attempt
import csv
with open ('Test.csv', newline='') as file:
reader = csv.reader(file)
list_a = list(reader)
new_list = [tuple(map(str,i)) for i in list_a]
print(new_list)
Current Output comes as list of strings
['(1,2,3)','(1,2,4)','(1,2,5)']
What is wrong with my approach? How can I solve this by not using ast.literal_eval(). I have used ast.literal_eval only to encounter difficulties as soon as file size increases.
来源:https://stackoverflow.com/questions/60270005/import-csv-file-as-a-list-of-tuples