問題描述
我使用這個 python shell 來生成一個字符串:
I use this python shell to generate a string:
>>>':'.join("{:x}
".format(random.randint(0, 2**16 - 1)) for i in range(4))
當(dāng)我在 Python2.7.5 中運行這個 shell 時,一切正常.但是當(dāng) Python 版本為 2.6.6
時會發(fā)生 ValueError: zero length field name in format
.當(dāng) Python 版本為 2.6.6
時,我應(yīng)該怎樣運行這個 shell?
When I run this shell in Python2.7.5, everything goes okay. But it occurs ValueError: zero length field name in format
when Python version is 2.6.6
. What should I run this shell fine when Python version is 2.6.6
?
推薦答案
在 Python 2.6 或更早的版本中,您需要顯式地為格式字段編號:
In Python versions 2.6 or earlier, you need to explicitly number the format fields:
':'.join("{0:x}
".format(random.randint(0, 2**16 - 1)) for i in range(4))
# ^
您可以在 docs 中閱讀相關(guān)內(nèi)容:
You can read about this in the docs:
在 2.7 版中更改:位置參數(shù)說明符可以省略,因此 '{} {}'
等效于 '{0} {1}'
.
Changed in version 2.7: The positional argument specifiers can be omitted, so
'{} {}'
is equivalent to'{0} {1}'
.
這篇關(guān)于ValueError:Python2.6.6 格式中的零長度字段名稱的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!