转换为字符串时,Python浮点数列表以“:"结尾 [英] Python list of floats ends up with a ':' when converting to a string

查看:127
本文介绍了转换为字符串时,Python浮点数列表以“:"结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从文本文件读取的浮点列表.经过一些数据处理后,我使用以下命令将列表写入文件:

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)

对于特定的输入文件,输出文件的字符串中将带有:". 我已经运行调试并在失败时停止了脚本.该值应为58710000.0

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

有什么建议吗?

推荐答案

在某些环境中,这是Python 2.7.3或更早版本中的错误.

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

用户@ecatmur在另一篇文章中指出了类似的问题,即'9' + 1 = ':' ASCII码

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

此问题已在 Python的更高版本中得到修复.
具体来说,该问题在 Python 2.7.5 中消失了,因此该问题已得到解决.

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

请参阅Gord Thompson接受的答案:

See Gord Thompson's accepted answer on:

相似的问题已关闭或未回答:

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

这篇关于转换为字符串时,Python浮点数列表以“:"结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆