I need to write lists that all differ in length to a CSV file in columns.
I currently have:
d=lists
writer = csv.writer(fl)
for values in zip(*d):
writer.writerow(values)
which works only partially. What I suspect is happening is that it stops zipping up until the lists with the list of the shortest length.
Code below is for Python 3. If you use Python 2, import izip_longest instead of zip_longest.
Result:
There's no 'neat' way to write it since, as you said, zip truncates to the length of the shortest iterable.
Probably the simplest way would be to just pad with None or empty strings (Not sure offhand what the behavior of writerow is with None values):
Alternatively, you could just construct each row inside the for loop instead of using
zip
, which would be more efficient but a lot wordier.EDIT: corrected example