I am trying to preallocate an empty array and at the same time defining the data type with a size of 19x5 using the following code:
import numpy as np
arr=np.empty((19,5),dtype=[('a','|S1'),('b', 'f4'),('c', 'i'),('d', 'f4'),('e', 'f4')])
The result is somewhat unexpected, yielding a 19*5*5 array.
However, trying:
arr=np.empty((19,1),dtype=[('a','|S1'),('b', 'f4'),('c', 'i'),('d', 'f4'),('e', 'f4')])
gives the proper length per row (5 fields), which apparently looks like a 1D array.
When I am trying to write this, only this formatting is allowed:
np.savetxt(file, arr, delimiter=',', fmt='%s')
This tells me I am dealing with a single string.
Is there no way to get a 19x5 shaped structured array that is not flattened?
The main problem arises when writing this with savetxt. I want to have a csv file that has all the 5 column values. As this is handled as a string it gives the wrong output.
See Question&Answers more detail:
os