bayesrule
bayesrule

Reputation: 125

Only one value in file, numpy.loadtxt() just returns the value instead of array?

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

Answers (2)

Mike Müller
Mike Müller

Reputation: 85432

Use ndmin=1:

numpy.loadtxt(fobj, ndmin=1)

to get a 1D array.

Upvotes: 2

John Zwinck
John Zwinck

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

Related Questions