Convert a lists in list to a CSV file by Python -
i hope convert data [[2,3,1],[11,2,4],[8,9,14]]
csv file , format (numbers in each line in different columns)
line1: 2 3 1 line2: 11 2 4 line3: 8 9 14
however, when use following codes (list
name of list should converted):
myfile = open('/users/user/desktop/list245.csv', 'wb') wr = csv.writer(myfile, quoting=csv.quote_all) wr.writerow(list)
the result is:
line 1: [2,3,1],[11,2,4],[8,9,14]
. namely, every numbers in first line different groups.
you need convert list of list list first.
from itertools import chain row = list(chain.from_iterable([[2,3,1],[11,2,4],[8,9,14]]))
then can write csv file
with open('/users/user/desktop/list245.csv', 'w') myfile: wr = csv.writer(myfile, quoting=csv.quote_all) wr.writerow(row)
Comments
Post a Comment