I have a numpy array of shape (444,445), and I need to dump it as a csv
file. One can achieve this by:
np.savetxt('outfile.txt',array, delimiter=',',fmt="%s")
I use the fmt="%s"
option, because at the end of each row (the 444 element of the array, is NaN
).
What I would like to accomplish is to write a csv
file that is 5 column wide, with 39,516 total lines (that is, 89 sections each of which consist of 5 columns and 444 lines), and finally the NaN
written as an empty element on the end of the 444th line. In this way, one has the number of elements of the matrix that are equivalent: 89x5x444=444x445
, or 197,580 pieces of data.
For instance:
1 xxxx,xxxx,xxxx,xxxx,xxxx,
2 xxxx,xxxx,xxxx,xxxx,xxxx,
...
...
89 xxxx,xxxx,xxxx,xxxx,
90 xxxx,xxxx,xxxx,xxxx,xxxx,
91 xxxx,xxxx,xxxx,xxxx,xxxx,
...
...
178 xxxx,xxxx,xxxx,xxxx,
I have added the line number to be more clear in my question. I do not want it in the actual output.
What would be an efficient and pythonic way of doing so?
For the moment, I am trying to adapt the answer to this question to my case: