Save lists to csv as columns in python -
i have 3 list i'd write .csv file, each list written column. example:
x = [0,1,2,3,4] y = [0,1,4,9,16] z = [1,1,1,1,1]
the file have structure:
0 0 1 1 1 1 2 4 1 3 9 1 4 16 1
i don't mind if delimiter is. tabs, commas etc fine.
i've tried like:
numpy.savetxt('file.csv',zip(x,y,z))
but creates 1d list alternating values of x, y , z, thought work?
thanks
you can below:
import csv open('file.csv', 'wb') csvfile: writer = csv.writer(csvfile, delimiter=',') i, j, k in zip(x, y, z): writer.writerow((i, j, k))
result:
>>> open('file.csv', 'r') csvfile: ... print(csvfile.read()) ... 0,0,1 1,1,1 2,4,1 3,9,1 4,16,1
Comments
Post a Comment