在具有不同行长的python中转置csv [英] Transpose csv in python with different row lengths
问题描述
我有许多具有可变长度行的 csv 文件.例如以下:
I have a number of csv files which have variable length rows. For example The following:
Time,0,8,18,46,132,163,224,238,267,303
X,0,14,14,14,15,16,17,15,15,15
Time,0,4,13,22,32,41,50,59,69,78,87,97,106,115,125,127,137,146,155,165,174,183,192,202,211,220,230,239,248,258,267,277,289,298,308
Y,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Time,0,4,13,22,32,41,50,59,69,78,87,97,106,115,125,127,137,146,155,165,174,183,192,202,211,220,230,239,248,258,267,277,289,298,308
Z,0,1,2,1,1,1,1,1,1,2,2,1,0,1,1,2,2,2,2,2,1,1,2,2,2,1,1,1,1,1,2,2,2,2,2
Time,0,308
W,0,0
变成:
Time,X,Time,Y,Time,Z,Time,W
0,0,0,0,0,0,0,0
8,14,4,0,4,1,308,0
丢失了很多数据,只用了前2个.
A lot of data has been lost, it only took the first 2 of each.
我想在 python 中转置这个 CSV.我有以下程序:
I want to transpose this CSV in python. I have the following program:
import csv
import os
from itertools import izip
import sys
try:
filename = sys.argv[1]
except IndexError:
print 'Please add a filename'
exit(-1)
with open(os.path.splitext(filename)[0] + '_t.csv', 'wb') as outfile, open(filename, 'rb') as infile:
a = izip(*csv.reader(infile))
csv.writer(outfile).writerows(a)
然而它似乎修剪了很多数据,因为文件从 20KB 降到了 6KB,并且只保持了最小行长度.
However it seems to trim a lot of data because the file has dropped from 20KB to 6KB and only keeps up to the minimum row length.
任何想法如何不丢失任何数据?
Any ideas how to not drop any data?
推荐答案
izip
根据最短数组压缩,因此您只能从每一行获取最短数组长度的值.
izip
zips according to the shortest array , so you are getting only the values from each row for the length of the shortest array.
您应该使用 izip_longest
而不是那个,它用最长的数组压缩,并且它会将 None 放在没有值的地方.
You should use izip_longest
instead of that , it zips with the longest array, and it would put None where there are no values.
示例 -
import csv
import os
from itertools import izip_longest
import sys
try:
filename = sys.argv[1]
except IndexError:
print 'Please add a filename'
exit(-1)
with open(os.path.splitext(filename)[0] + '_t.csv', 'wb') as outfile, open(filename, 'rb') as infile:
a = izip_longest(*csv.reader(infile))
csv.writer(outfile).writerows(a)
我从中得到的结果 -
Time,X,Time,Y,Time,Z,Time,W
0,0,0,0,0,0,0,0
8,14,4,0,4,1,308,0
18,14,13,1,13,2,,
46,14,22,1,22,1,,
132,15,32,1,32,1,,
163,16,41,1,41,1,,
224,17,50,1,50,1,,
238,15,59,1,59,1,,
267,15,69,1,69,1,,
303,15,78,1,78,2,,
,,87,1,87,2,,
,,97,1,97,1,,
,,106,1,106,0,,
,,115,1,115,1,,
,,125,1,125,1,,
,,127,1,127,2,,
,,137,1,137,2,,
,,146,1,146,2,,
,,155,1,155,2,,
,,165,1,165,2,,
,,174,1,174,1,,
,,183,1,183,1,,
,,192,1,192,2,,
,,202,1,202,2,,
,,211,1,211,2,,
,,220,1,220,1,,
,,230,1,230,1,,
,,239,1,239,1,,
,,248,1,248,1,,
,,258,1,258,1,,
,,267,1,267,2,,
,,277,1,277,2,,
,,289,1,289,2,,
,,298,1,298,2,,
,,308,1,308,2,,
这篇关于在具有不同行长的python中转置csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!