Import csv file as a list of tuples

牧云@^-^@ 提交于 2020-02-26 04:14:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!