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

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

对于特定的输入文件,输出文件的字符串中将带有“:”。
我已经运行调试并在失败时停止了脚本。该值应为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

仅对于某些文件和 float 会发生这种情况

有什么建议么?

最佳答案

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

用户@ecatmur在另一篇文章中指出了类似的问题,即ASCII中的'9' + 1 = ':'
在更高版本的Python中已修复
具体来说,该问题在 Python 2.7.5 中消失了,因此该问题已得到解决。

请参阅Gord Thompson在以下方面的公认答案:

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

  • 已关闭或未回答类似问题:
  • 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浮点数列表以 ':'结尾,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14226486/

    10-09 17:11
    查看更多