簡體   English   中英

如何創建每個CSV行的子列表並將該子列表放在列表中

[英]How do i make a sublist of every CSV row and put that sublist inside a list

我正在制作一個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]

最簡單的方法是這樣的

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將是一個列表清單

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM