ValueError:Python2.6.6 格式的零长度字段名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21034954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:52:12  来源:igfitidea点击:

ValueError: zero length field name in format in Python2.6.6

pythonformat

提问by changzhi

I use this python shell to generate a string:

我使用这个 python shell 来生成一个字符串:

>>>':'.join("{:x}\n".format(random.randint(0, 2**16 - 1)) for i in range(4))

When I run this shell in Python2.7.5, everything goes okay. But it occurs ValueError: zero length field name in formatwhen Python version is 2.6.6. What should I run this shell fine when Python version is 2.6.6?

当我在 Python2.7.5 中运行这个 shell 时,一切正常。但是ValueError: zero length field name in format当 Python 版本是2.6.6. 当 Python 版本为 时,我应该如何运行这个 shell 2.6.6

采纳答案by changzhi

In Python versions 2.6 or earlier, you need to explicitly number the format fields:

在 Python 2.6 或更早版本中,您需要显式编号格式字段:

':'.join("{0:x}\n".format(random.randint(0, 2**16 - 1)) for i in range(4))
#          ^

You can read about this in the docs:

您可以在文档中阅读有关此内容的信息:

Changed in version 2.7: The positional argument specifiers can be omitted, so '{} {}'is equivalent to '{0} {1}'.

在 2.7 版更改: 可以省略位置参数说明符,因此'{} {}'等效于'{0} {1}'