Writing a csv header deletes data from the numpy array written below

I am trying to export data to a csv file. It should contain a header (from datastack) and restyled arrays with my data (from datastack). One row in a datastack is the same length as a dataset. The code below works, but it removes parts of the first row from the datastack. Any ideas why this could be?

s = ','.join(itertools.chain(dataset)) + '\n' newfile = 'export.csv' f = open(newfile,'w') f.write(s) numpy.savetxt(newfile, (numpy.transpose(datastack)), delimiter=', ') f.close() 
+4
source share
1 answer

You add a file twice with the file name export.csv, once when you call open() and once when you call numpy.savetxt() . Thus, there are two open files that compete for the same file name. If you pass the file descriptor, and not the file name, to numpy.savetxt() , you will avoid this race condition:

 s = ','.join(itertools.chain(dataset)) + '\n' newfile = 'export.csv' f = open(newfile,'w') f.write(s) numpy.savetxt(f, (numpy.transpose(datastack)), delimiter=', ') f.close() 
+6
source

Source: https://habr.com/ru/post/1309401/


All Articles