Reputation: 125
When only one value in file, numpy.loadtxt() just returns the value instead of array, how to avoid?
Thank you in advance!
e.g. there's only 12345 in a file
12345.6
numpy.loadtxt() returns
12345.6
instead of
array([12345.6])
Upvotes: 4
Views: 1842
Reputation: 85432
Use ndmin=1
:
numpy.loadtxt(fobj, ndmin=1)
to get a 1D array.
Upvotes: 2
Reputation: 249093
Use the ndmin
parameter of numpy.loadtxt
. Set it to 1, and you will always have an array with at least one dimension.
Upvotes: 6