Python 3.0、3.1、3.2中的“ ValueError:格式为零长度的字段名称”错误

问题:Python 3.0、3.1、3.2中的“ ValueError:格式为零长度的字段名称”错误

我正在尝试学习Python(具体来说是3),并且遇到了以下错误:

ValueError: zero length field name in format

我用谷歌搜索,发现需要指定数字:

a, b = 0, 1
if a < b:
     print('a ({0}) is less than b ({1})'.format(a, b))
else:
     print('a ({0}) is not less than b ({1})'.format(a, b))

而且不像本教程(来自lynda.com)实际所说的那样:

a, b = 0, 1
if a < b:
     print('a ({}) is less than b ({})'.format(a, b))
else:
     print('a ({}) is not less than b ({})'.format(a, b))

以下教程即时消息具有Python 3.1,即时消息使用3.2,而我读到的有关此错误的信息是,此错误仅发生在<3.1(3.0)中。他们在3.2中撤消了此操作,还是我做错了什么?

另外,说慢点;)从字面上看,这是我第一次学习Python,只是我用Python编写的第二个“脚本”。

I’m trying learn Python (3 to be more specific) and I’m getting this error:

ValueError: zero length field name in format

I googled it and I found out you need to specify the numbers:

a, b = 0, 1
if a < b:
     print('a ({0}) is less than b ({1})'.format(a, b))
else:
     print('a ({0}) is not less than b ({1})'.format(a, b))

And not like the tutorial (from lynda.com) actually says to do:

a, b = 0, 1
if a < b:
     print('a ({}) is less than b ({})'.format(a, b))
else:
     print('a ({}) is not less than b ({})'.format(a, b))

The tutorial im following has Python 3.1, and im using 3.2 and what i read about this error is that this only happens in <3.1 (3.0). Did they undo this in 3.2, or am i doing something wrong?

Also, speak slowly ;) this is literally my first night learning Python and only the 2nd “script” i’ve written in Python.


回答 0

我会猜测您偶然以某种方式运行了python 2.6。

如果您使用的是python 3,则此功能至少适用于3.1;如果您使用的是python 2,则此功能仅适用于2.7。

I’m gonna guess that you are running python 2.6 by accident somehow.

This feature is only available for at least 3.1 if you are using python 3, or 2.7 if you are using python 2.


回答 1

Python 2.6和3.0需要字段编号。在Python 2.7和更高版本以及3.1和更高版本中,可以忽略它们。

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

python2.6.4>>> print '|{0:^12}|{1:^12}|'.format(3,4)
|     3      |     4     |

Python 2.6 and 3.0 require the field numbers. In Python 2.7 and later and 3.1 and later, they can be omitted.

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

python2.6.4>>> print '|{0:^12}|{1:^12}|'.format(3,4)
|     3      |     4     |

回答 2

如果您使用的是Eclipse,则应查看Window-> Preferences-> PyDev-> Interpreter-Python。那里有口译员的名单(包括姓名和位置)。如果对于当前项目,您正在使用例如位于/ usr / bin / python中的解释器,则可能执行/ usr / bin / python -V whill会给您类似“ Python 2.6.6”的信息。就像Winston Ewert所写的那样,您的答案是正确的。

(您可以通过单击“新建…”按钮并在/ usr / bin / python3中将其添加为“位置”来添加新的交互程序。然后,您可能必须更改项目设置(首选项-> PyDev-解释器/语法)。

If you’re using Eclipse you should look into Window -> Preferences -> PyDev -> Interpreter – Python. There you have a list of interpreters (with name and location). If for your current project you’re using interpreter which is located for example in /usr/bin/python then probably executing /usr/bin/python -V whill give you something like “Python 2.6.6”. And there is your answer like Winston Ewert wrote.

(you can add new interperter by simply clicking “New…” button and giving /usr/bin/python3 as “location”. Then you have probably to change your project settings (Preferences -> PyDev – Interpreter/Grammar).