Skip to content Skip to sidebar Skip to footer

How To Export Data From Csv As A List(python 3)

I have a list like this(python 3) my_list = [['xxx','moon',150],['wordq','pop',3]] and i save it on a csv using this code import csv myfile = open('pppp.csv', 'wb') with open('pp

Solution 1:

Just convert the data you get from reader() to a list:

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

Solution 2:

Unless you have a reason why you are using newline='', you can skip that and below code works with 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 indata:
    print row

Post a Comment for "How To Export Data From Csv As A List(python 3)"