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

霸气de小男生 提交于 2019-12-01 18:15:05

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:

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!