Python ValueError:无法从手动字段规范切换到自动字段编号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46768088/
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
ValueError: cannot switch from manual field specification to automatic field numbering
提问by Calculus
The class:
班上:
class Book(object):
def __init__(self, title, author):
self.title = title
self.author = author
def get_entry(self):
return "{0} by {1} on {}".format(self.title, self.author, self.press)
Create an instance of my book from it:
从中创建我的书的一个实例:
In [72]: mybook = Book('HTML','Lee')
In [75]: mybook.title
Out[75]: 'HTML'
In [76]: mybook.author
Out[76]: 'Lee'
Please notice that I didn't initialize attribute 'self.press',while use it in the get_entry method.Go ahead to type in data.
请注意,我没有初始化属性 'self.press',而是在 get_entry 方法中使用它。继续输入数据。
mybook.press = 'Murach'
mybook.price = 'download'
Till now, I can specify all the data input with vars
到目前为止,我可以指定所有数据输入 vars
In [77]: vars(mybook)
Out[77]: {'author': 'Lee', 'title': 'HTML',...}
I hardtype lot of data about mybook in the console.When try to call get_entry method, errors report.
我在控制台硬输入了很多关于mybook的数据。当尝试调用get_entry方法时,错误报告。
mybook.get_entry()
ValueError: cannot switch from manual field specification to automatic field numbering.
All this going in interactive mode on console.I cherish the data inputed, further to pickle mybook
object in file. However, it is flawed. How can rescue it in the interactive mode.
or I have to restart all over again.
所有这些都在控制台上以交互模式进行。我珍惜输入的数据,进一步mybook
在文件中腌制对象。然而,它是有缺陷的。如何在交互模式下拯救它。或者我必须重新开始。
回答by Jean-Fran?ois Fabre
return "{0} by {1} on {}".format(self.title, self.author, self.press)
that doesn't work. If you specify positions, you have to do it through the end:
那行不通。如果你指定位置,你必须做到底:
return "{0} by {1} on {2}".format(self.title, self.author, self.press)
In your case, best is to leave python treat that automatically:
在您的情况下,最好让 python 自动处理:
return "{} by {} on {}".format(self.title, self.author, self.press)