我如何制作每个 CSV 行的子列表并将该子列表放入列表中

我正在为 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]


尚方宝剑之说
浏览 180回答 1
1回答

撒科打诨

最简单的方法是这样的import csvpathtocsv='path/to/file'with open(pathtocsv,'r') as cfile:    creader=csv.reader(cfile,delimiter=',')    wines_list=list(creader)print(wines_list)然后wines_list将是一个列表列表
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python