[Image-SIG] How do i import text file which has NaNs ?

Steve Holden steve at holdenweb.com
Wed Dec 5 22:01:50 CET 2007


Christopher Barker wrote:
>> The ASCII file has NaNs or NoData in them.
> 
>> I am not able to read the file into python since it is not able to 
>> recognize NaN.
> 
>> */[jv] I’m not sure this is applicable, but have you looked at the numpy 
>> extension package which has support for NaN?/*
> 
> numpy does support NaN, and it's an excellent choice for this kind of 
> data anyway. It's also easy to turn a numpy array into a PIL image.
> 
> However, I'm not sure it supports NaN text literals, and, if it does, 
> it's probably not platform independent, as it uses the system library 
> scanf().
> 
> I would check out numpy, but you may have to write the file scanning 
> code yourself, and translate the NaN.
> 
> An example might help us get other ideas. For instance, what is the 
> ascii representation of NaN? "NaN", or something else?
> 
> What system are you running on? For instance, on OS-X (Python 2.5, 
> compiled with gcc4.0.1), this works:
> 
>  >>> float("NaN")
> nan
> 
> But I don't think that works on the stock Windows Python. However, there 
> may be another literal that does. Try:
> 
>  >>> import numpy as N
>  >>> str(N.nan)
> 'nan'
> 
> and it might tell you the literal on your system.
> 
> -Chris
> 
Sadly not:

 >>> import numpy
 >>> str(numpy.nan)
'-1.#IND'
 >>> float(str(numpy.nan))
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): -1.#IND
 >>> repr(numpy.nan)
'-1.#IND'
 >>>

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/


More information about the Image-SIG mailing list