我正在为一个csv文件中的每一行创建一个子列表,但是我又得到了一个列表
wines_list = []
for row in wines:
wines_list.append(row)
print(wines_list)
这将返回:
['id', 'country', 'description', 'designation', 'points', 'price',
'province', 'taster_name', 'title', 'variety', 'winery', 'fixed acidity',
'volatile acidity', 'citric acid', 'residual sugar', 'chlorides', 'free
sulfur dioxide', 'total sulfur dioxide', 'density', 'pH', 'sulphates',
'alcohol']
但是我不希望将所有值附加到子列表,并将其附加到wines_list
所以我希望wines_list变得像这样:
[[1, 'netherlands', 'description of the wine', 'designation', 'points', 'price',
'province', 'taster_name', 'title', 'variety', 'winery', 'fixed acidity',
'volatile acidity', 'citric acid', 'residual sugar', 'chlorides', 'free
sulfur dioxide', 'total sulfur dioxide', 'density', 'pH', 'sulphates',
'alcohol'], ANOTHER SUB LIST HERE]
答案 0 :(得分:0)
最简单的方法是这样
import csv
pathtocsv='path/to/file'
with open(pathtocsv,'r') as cfile:
creader=csv.reader(cfile,delimiter=',')
wines_list=list(creader)
print(wines_list)
然后wines_list
将是列表列表