Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python list of floats ends up with a ':' when converting to a string

Tags:

python

list

colon

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?

like image 670
Dave B Avatar asked Oct 05 '22 10:10

Dave B


1 Answers

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:

  • Python inserts a colon in a decimal number from Access via PyoDBc

Similar questions were closed or not answered:

  • Python float got a colon after the decimal point after addition
  • Python list of floats ends up with a ':' when converting to a string
like image 190
pashute Avatar answered Oct 10 '22 03:10

pashute