我有一个类似的列表:
[('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')]
其中'CarrierDel','CarrierID'和'Delays'是我的标题。我想把它写成一个csv文件。有没有相当于作家,但对于列?或者还有另一种方法可以解决这个问题吗?
谢谢!
答案 0 :(得分:4)
import csv
w = csv.writer(open("some_file.txt","w"))
w.writerows(zip(*list_of_cols))
应该这样做
zip转换二维数组(以及其他内容),如下所示
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)]