Python: “List.append = ‘list’对象属性“append”是只读的ÉD;
我正在尝试将 Solr 服务器的响应写入 CSV 文件。我对 python 很陌生,并且已经给出了需要修改的代码。最初的代码看起来像这样......
for doc in response.results:
status = json.loads(doc['status'])
脚本运行并打印正确的信息。但它只打印一个结果(最后一个)。我认为这是因为循环不断地写入变量“状态”,直到它完成响应。
经过一番阅读后,我决定将信息存储在列表中。这样我就可以将信息打印到列表中的单独行中。我创建了一个空列表并更改了下面的代码 -
for doc in response.results:
list.append = json.loads(doc['status'])
尝试运行代码后我收到了此响应 -
`AttributeError: 'list' object attribute 'append' is read-only`.
我哪里出错了?列表不是最好的方法吗?
I’m trying to write a response from a Solr server to a CSV file. I’m pretty new to python and have been given code to modify. Originally the code looked like this ...
for doc in response.results:
status = json.loads(doc['status'])
The script runs and prints the correct information. But it only every prints one result (last one). I think this is because the loop constantly writes over the varible 'status' until its worked through the response.
After some reading I decided to store the information in a list. That way i could print the information to seprate lines in a list. I created an empty list and changed the code below -
for doc in response.results:
list.append = json.loads(doc['status'])
I got this response back after trying to run the code -
`AttributeError: 'list' object attribute 'append' is read-only`.
Where am I going wrong? Is a list not the best approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在尝试修改内置
list
类的append
方法!只需执行
或等效操作:
You're trying to modify the
append
method of the built-inlist
class!Just do
or equivalently:
我不确定你想做什么。
我猜您还没有创建
list
变量。list
是 python 的内置列表类,因此如果没有变量来屏蔽它,您将访问它。并且您尝试修改它的其中一个属性,这是不允许的(它不像 ruby,您可以对任何内容进行猴子修补)。这是你想要的吗? :
I'm not sure what you are trying to do.
I guess you haven't created a
list
variable.list
is a python's builtin class for lists, so if there's no variable to mask it, you'll access that. And you tried to modify one of it's propterties, which is not allowed (it's not like ruby where you can monkey-patch anything).Is this what you want? :
尝试
Try