[Pythonmac-SIG] Bug in float() ??

Chris Barker cbarker@jps.net
Mon, 05 Apr 1999 18:03:07 -0700


Hi,

I spend a lot of time parsing out formatted text files, and have
encountered the following problem:

consider this code:

import string

line = '34.23, 45.43 , 987,   654  '
print line

data = string.split(line,',')
print data

data = map(float,data)
print data

I run this on my Linux box, and it works just great:

34.23, 45.43 , 987,   654
['34.23', ' 45.43 ', ' 987', '   654  ']
[34.23, 45.43, 987.0, 654.0]  

I run in on my Mac, and I get the following:

34.23, 45.43 , 987,   654  
['34.23', ' 45.43 ', ' 987', '   654  ']
Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "Chris's Machine:Python 1.5.1:test_float.py", line 8, in ?
    data = map(float,data)
ValueError: null byte in argument for float()

Clearly, float() doesn't know what to do with the extra spaces that
string.split() left in. I know I can just use string.strip() as well,
but what bugs me is that I really need my code to be platform
independent, so it's a real pain  to have code that works on Linux not
work on the Mac.

By the way, What I really need is a smart and fast formatted file
reader, something like MATLAB's fscanf. Anyone want to help me with
that?

-Chris



-- 
Christopher Barker,
Ph.D.                                                           
cbarker@jps.net                      ---           ---           ---
http://www.jps.net/cbarker          -----@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Water Resources Engineering       ------   @    ------   @   ------   @
Coastal and Fluvial Hydrodynamics -------      ---------     --------    
------------------------------------------------------------------------
------------------------------------------------------------------------