4

I have a list of floats read in from a text file. After some data processing I write the list to a file using:

for val in flist:
    sa = '{0}'.format(val)
    fout.write(sa)

For specific input files, the output file will have a ':' in the string. I have run debug and stopped the script at the point of failure. The value should be 58710000.0

[Dbg]>>> print val[464]
   5870:000.0
[Dbg]>>> fa = val[464]
[Dbg]>>> print fa
  5870:000.0
[Dbg]>>> 
[Dbg]>>> fa = fa + 1
[Dbg]>>> print fa
   58710001.0
[Dbg]>>> fa = fa - 1
[Dbg]>>> print fa
   5870:000.0

This happens only for certain files and floats

Any suggestions?

dan1st
  • 12,568
  • 8
  • 34
  • 67
Dave B
  • 49
  • 1

3 Answers3

4

It's a bug in Python 2.7.3 or perhaps earlier, with certain environments.

User @ecatmur pointed out in a different post with a similar question, that '9' + 1 = ':' in ASCII

This has been Fixed in later versions of Python.
Specifically, the problem disappeared in Python 2.7.5 so the issue has been fixed.

See Gord Thompson's accepted answer on:

Similar questions were closed or not answered:

pashute
  • 3,965
  • 3
  • 38
  • 65
  • 2
    Would be nice with a reference to the Python change that did fix something. AFAICT this is a C library bug, not internal to CPython. – Yann Vernier Sep 11 '17 at 10:48
0

Your values are probably not actually floats. Anyhow, sa = '{0}'.format(val) is silly; you might as well do just sa = str(val). But if you do sa = '%f' % (val,) instead, you will get an exception when val is not a float.

Phil Frost
  • 3,668
  • 21
  • 29
0

This appears to be highly platform specific. The repr for float calls PyOS_double_to_string which ends up calling PyOS_snprintf, which wraps snprintf with some code to make that function more consistent across platforms. It appears on some version of AIX in particular, snprintf can produce 0: instead of 10.

Could you share your sys.platform and sys.version values? (The version header printed when you start an interactive python interpreter should do.)

Relevant source files: Python/mysnprintf.c for PyOS_snprintf, Objects/floatobject.c for float_repr, Python/pystrtod.c for PyOS_string_to_double.

Yann Vernier
  • 15,414
  • 2
  • 28
  • 26