Python zip object is not written in CSV file

Question:

This question is the same as this, but I am interested in why this is a problem rather than a work-around to solve it:

I have some data with each category in one list, then zip it and then write it to a CSV file. This gives an empty CSV-file without any error or warning. Below, there is a sample code with different versions and I would like to understand why things work or not, what is the logic of it?

import csv
import numpy as np

#original version
hist_data = [[0, 1, 'r'], [0, 0, 5], [np.array([11]),np.array([12]),np.array([13])], [0.22946621533609002, 0.21773800579739627, 0.1881331243203004], ['NaN', 'NaN', 'NaN'], ['NaN','NaN', True], [1678615482.6110842, 1678615504.0591152, 1678615507.720184], [['some','list'],['more','data'], 'NaN'], [0, 1, None]]

zipped = zip(*hist_data)
converted_zipped = [[item for item in z] for z in list(zipped)]
list1=[[1,2,3],[4,5,7]]
tuple_list=[('a','b','c'),('d','e','f')]

filename='./zip_test.csv'
with open(filename,'w') as outputfile:
   writer=csv.writer(outputfile, delimiter=';')
   # version a)
   writer.writerows(zipped) # -> does not work
   #version b)
   for row in zipped # -> as proposed in linked so question
      print('row:', row) #-> does not print anything
      writer.writerow(row) #-> does not work
   #version c)
   writer.writerows(list1) # -> works
   #version d)
   writer.writerows(tuple_list) # -> works
   #version e)
   writer.writerows(converted_zipped) # -> works
   # f)
   writer. writerow(zipped) # -> does not work

Comment: I use python 3.8.10. I have some older code where I used version a) and it worked (python 3.6). Is there a connection to the python version?

Asked By: qcabepsilon

||

Answers:

zip() returns an iterator and in your next line (converted_zipped = ...) you consume this iterator. So when you next call writer.writerows(zipped) you will write nothing.

Consider this example:

import csv
import numpy as np

hist_data = [[0, 1, 'r'], [0, 0, 5], [np.array([11]),np.array([12]),np.array([13])], [0.22946621533609002, 0.21773800579739627, 0.1881331243203004], ['NaN', 'NaN', 'NaN'], ['NaN','NaN', True], [1678615482.6110842, 1678615504.0591152, 1678615507.720184], [['some','list'],['more','data'], 'NaN'], [0, 1, None]]

zipped = zip(*hist_data)

filename='./zip_test.csv'
with open(filename,'w') as outputfile:
   writer=csv.writer(outputfile, delimiter=';')
   writer.writerows(zipped)

Since you use the iterator zipped only once (in writer.writerows) the file with following content is created:

0;0;[11];0.22946621533609002;NaN;NaN;1678615482.6110842;['some', 'list'];0
1;0;[12];0.21773800579739627;NaN;NaN;1678615504.0591152;['more', 'data'];1
r;5;[13];0.1881331243203004;NaN;True;1678615507.720184;NaN;
Answered By: Andrej Kesely
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.