向Python 2.6反向移植" {n:,d}" .format()

时间:2015-03-18 17:18:24

标签: python formatting

在v2.7中添加了Python format中的“千位分隔符”("{n:,d}"中的逗号):

$ python2.7 -c 'print "{n:,d}".format(n=1234567890)'
1,234,567,890

我有很多使用它的代码,我必须在2.6下运行(因为这是CentOS 6.4发布的)并且现在失败了:

$ python2.6 -c 'print "{n:,d}".format(n=1234567890)'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ValueError: Invalid conversion specification

我的选择是什么?

StringFormat fails

>>> import stringformat
>>> stringformat.init(True)
>>> "{n:,d}".format(n=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/site-packages/stringformat.py", line 326, in format
    return FormattableString(self).format(*args, **kwargs)
  File "/usr/lib/python2.6/site-packages/stringformat.py", line 267, in format
    want_bytes)
  File "/usr/lib/python2.6/site-packages/stringformat.py", line 159, in _format_field
    value = value.__format__(spec)
ValueError: Invalid conversion specification

0 个答案:

没有答案