ValueError:Python2.6.6 格式中的零长度字段名称 [英] ValueError: zero length field name in format in Python2.6.6

查看:20
本文介绍了ValueError:Python2.6.6 格式中的零长度字段名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个 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))

当我在 Python2.7.5 中运行这个 shell 时,一切正常.但是当 Python 版本为 2.6.6 时会发生 ValueError: zero length field name in format.当 Python 版本为 2.6.6 时,我应该怎样运行这个 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 中阅读相关内容:

You can read about this in the docs:

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

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

这篇关于ValueError:Python2.6.6 格式中的零长度字段名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆