Transpose csv in python with different row lengths -


i have number of csv files have variable length rows. example 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 

becomes:

time,x,time,y,time,z,time,w 0,0,0,0,0,0,0,0 8,14,4,0,4,1,308,0 

a lot of data has been lost, took first 2 of each.

i want transpose csv in python. have following program:

import csv import os itertools import izip import sys  try:     filename = sys.argv[1] except indexerror:     print 'please add filename'     exit(-1) open(os.path.splitext(filename)[0] + '_t.csv', 'wb') outfile, open(filename, 'rb') infile:     = izip(*csv.reader(infile))     csv.writer(outfile).writerows(a) 

however seems trim lot of data because file has dropped 20kb 6kb , keeps minimum row length.

any ideas how not drop data?

izip zips according shortest array , getting values each row length of shortest array.

you should use izip_longest instead of , zips longest array, , put none there no values.

example -

import csv import os itertools import izip_longest import sys  try:     filename = sys.argv[1] except indexerror:     print 'please add filename'     exit(-1) open(os.path.splitext(filename)[0] + '_t.csv', 'wb') outfile, open(filename, 'rb') infile:     = izip_longest(*csv.reader(infile))     csv.writer(outfile).writerows(a) 

result got -

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,, 

Comments