I am parsing a datafile which contains white-space delimited text which was generated from c++. Some of the driving computations will overflow, underflow or generate NaN’s. It appears that the the strings “1.#INF00” and “1.#IND00” are not digested by numpy.array(), returning an “invalid literal for float()” error. I have attempted making a substitution like this:
line = line.replace('1.#INF00','inf')
line = line.replace('1.#IND00','ind')
vals = line.split(' ')
myarray = array(vals)
but alas, to no avail. I have also tried ‘nan’ and ‘NaN’. Is there some string i could substitute which float() will interpret into a nan, inf etc? Perhaps I need to escape in some quotes?
As an aside, can you tell me how matplotlib will handle an inf?
The default solution would be to change them to NaN’s when they are detected. I have found it demonstrated that matplotlib will handle those gracefully, leaving gaps in the data. Which would be acceptable treatment for my ‘inf’s and ‘ind’s
float('nan')should return NaN andfloat('inf')should return infinity. At least that’s how they work on my interpreter (CPython 2.7). It seems like things were different on some platforms (especially on Windows) back in CPython 2.5, but I doubt you’re using such an old version of Python.Maybe the problem is with numpy, but in that case you can try: