Python: “List.append = ‘list’对象属性“append”是只读的ÉD;

发布于 2025-01-07 05:38:03 字数 555 浏览 0 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

思慕 2025-01-14 05:38:03
>>> list.append
<method 'append' of 'list' objects>

您正在尝试修改内置 list 类的 append 方法!

只需执行

docstats = []
for doc in response.results:
    docstats.append(json.loads(doc['status']))

或等效操作:

docstats = [json.loads(doc['status']) for doc in response.results]
>>> list.append
<method 'append' of 'list' objects>

You're trying to modify the append method of the built-in list class!

Just do

docstats = []
for doc in response.results:
    docstats.append(json.loads(doc['status']))

or equivalently:

docstats = [json.loads(doc['status']) for doc in response.results]
冰葑 2025-01-14 05:38:03

我不确定你想做什么。

我猜您还没有创建 list 变量。 list 是 python 的内置列表类,因此如果没有变量来屏蔽它,您将访问它。并且您尝试修改它的其中一个属性,这是不允许的(它不像 ruby​​,您可以对任何内容进行猴子修补)。

这是你想要的吗? :

l=[]
for doc in response.results:
    l.append(json.loads(doc[‘status’]))

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? :

l=[]
for doc in response.results:
    l.append(json.loads(doc[‘status’]))
长发绾君心 2025-01-14 05:38:03

尝试

list.append(json.loads(doc['status']))

Try

list.append(json.loads(doc['status']))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文