Reputation: 9432
I have the problem, that I wrote code which is using the following numpy calls
columnNames = ['A','B','C'];
dt = [(s,np.float64) for s in columnNames];
# load structured unit
SimData = np.loadtxt( file ,dtype=dt, delimiter="\t", comments='#')
If my file contains only one line, then SimData['A'][0]
does not exist because the command returns a 0-d array, this is somehow annoying?
How can I make my code using indexing which also works for single line files?
Upvotes: 1
Views: 579
Reputation: 114781
You can use the argument ndmin=1
to force loadtxt
to return a result that is at least one-dimensional:
In [10]: !cat data.tsv
100 200 300
In [11]: a = np.loadtxt('data.tsv', dtype=dt, delimiter='\t', ndmin=1)
In [12]: a.shape
Out[12]: (1,)
In [13]: a
Out[13]:
array([(100.0, 200.0, 300.0)],
dtype=[('A', '<f8'), ('B', '<f8'), ('C', '<f8')])
Upvotes: 4