Reputation: 2187
I have a list that looks like:
[('CarrierDel', '1973.0', '4227.0', '518.0', '2701.0', '3249.0', '7354.0', '980.0', '428.0', '394.0'), ('CarrierID', '9E', 'AA', 'AS', 'B6', 'DL', 'EV', 'F9', 'FL', 'HA'), ('Delays', '3626.0', '8394.0', '1071.0', '3880.0', '5927.0', '13836.0', '1461.0', '1254.0', '303.0')]
where 'CarrierDel', 'CarrierID' and 'Delays' are my headers. I want to write this to a csv file. Is there an equivalent to writerow but for columns? Or is there another way to go about this?
Thanks!
Upvotes: 0
Views: 2041
Reputation: 113930
import csv
w = csv.writer(open("some_file.txt","w"))
w.writerows(zip(*list_of_cols))
should do it
zip transposes a 2d array(among other things) as seen below
a = [[1,2,3],[4,5,6],[7,8,9]]
#transpose a with zip
print zip(*a) #prints [(1,4,7),(2,5,8),(3,6,9)]
Upvotes: 4