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

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

问题描述

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

I use this python shell to generate a string:

>>>':'.join("{:x}\n".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?

推荐答案

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

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

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

您可以在文档中了解此内容:

You can read about this in the docs:

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

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

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