简体   繁体   English

如何将csv中的数据导出为列表(python 3)

[英]How to export data from csv as a list(python 3)

I have a list like this(python 3) 我有一个这样的列表(python 3)

my_list = [["xxx","moon",150],["wordq","pop",3]]

and i save it on a csv using this code 我使用此代码将其保存在csv上

import csv

myfile = open("pppp.csv", 'wb')
with open("pppp.csv", "w", newline='') as myfile:
    wr = csv.writer(myfile, quoting=csv.QUOTE_NONE)
    wr.writerows(list_of_DVDsuppliers)

now i need to export this csv in to my program as a list and change the data . 现在我需要将此csv作为列表导出到我的程序中并更改数据。 please help me ? 请帮我 ?

Just convert the data you get from reader() to a list: 只需将您从reader()获得的数据转换为列表即可:

data = csv.reader(open('example.csv','r'))
data = list(data)
print data

Unless you have a reason why you are using newline='', you can skip that and below code works with python 2.7, 除非您有使用newline =''的理由,否则可以跳过,下面的代码适用于python 2.7,

import csv

my_list = [["xxx","moon",150],["wordq","pop",3]]

myfile = open("pppp.csv", 'wb')
with open("pppp.csv", "w") as myfile:
    wr = csv.writer(myfile, quoting=csv.QUOTE_NONE)
    wr.writerows(my_list)


data = csv.reader(open('pppp.csv','r'))
for row in data:
    print row

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM