如何将csv文件转换为列表列表,其中每一行都是较大列表中的条目列表?
我遇到了麻烦,因为我的一些条目中有逗号
像:
这样的文件'1','2','3'
'1,1','2,2','3,3'
成为:
[['1','2','3']['1','1','2','2','3','3']]
而不是:
[['1','2','3']['1,1','2,2','3,3']
答案 0 :(得分:0)
in your data '2,2' is one string. But csv reader can deal with this:
import csv
result = []
with open("data", 'r') as f:
r = csv.reader(f, delimiter=',')
# next(r, None) # skip the headers
for row in r:
result.append(row)
print(result)
[["'1'", "'2'", "'3'"], ["'1", "1'", "'2", "2'", "'3", '3']]