[Numpy-discussion] numpy.fromstring() : error handling ?

Christopher Barker Chris.Barker at noaa.gov
Sat Aug 9 02:18:29 EDT 2008


oc-spam66 wrote:
> Is it really how it should be ?
> 
> I thought this method should be restrictive in what it accepts (because 
> the goal is to fill an array with precise dimensions).

if you know how big an array you want, then you should use the "count" 
keyword:

np.fromstring("45.6 76.4", dtype=np.float, sep = ' ', count = 1)

> When floating 
> point numbers are mixed with something else, it should be parsed with a 
> regexp.

Or any number of other ways. Yes, fromstring (and from file) is for only 
the simplest of needs, very efficiently pulling a bunch of numbers from 
text.

In any case, your example shows a bug, plain and simple. Parsing 
"kjhsdf" should not produce the number zero, period. I suppose what it 
should do it up for debate, it could either raise an exception or return 
an empty array. By the way, I've found another bug:

 >> np.fromstring("23 ", dtype=np.float, sep=" ", count=2)

array([  3.400000000e+01,   1.09284155e-305])

That really should raise an exception rather than returning garbage!

Oh well, it looks like there is some work to be done.

-Chris


-- 
Christopher Barker, Ph.D.
Oceanographer

NOAA/OR&R/HAZMAT         (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception



More information about the NumPy-Discussion mailing list