Python: "List.append =" object list attribute "append is read-only"

I am trying to write a response from a Solr server in a CSV file. Im pretty new to python and got the code to change. The code initially looked like this ...

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

The script runs and prints the correct information. But it is only everyone who prints one result (the last). I think this is because the loop is constantly writing over the changed status until it processes the response.

After some reading, I decided to keep the information in a list. That way I could print the information to highlight the lines in the 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 answer after trying to run the code -

 `AttributeError: 'list' object attribute 'append' is read-only`. 

Where am I mistaken? Is a list not the best approach?

+5
source share
4 answers
 >>> list.append <method 'append' of 'list' objects> 

You are trying to change the append method of the list inline 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] 
+7
source

I'm not sure what you are trying to do.

I think you did not create the list variable. list is a built-in python class for lists, so if there is no variable to mask it, you will get access to it. And you tried to modify one of your prominences, which is not allowed (this does not look like a ruby, where you can decapitate something).

Is this what you want?

 l=[] for doc in response.results: l.append(json.loads(doc['status'])) 
+1
source

Try

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

Hello, I am in the same situation as @Chris Chalmers means that I get the same error when using the add operator, but I do not use any built-in names. My code looks like this:

Everyone in the for OP=[] # loop defined an empty list, now between the programs I compute the point Centroid = [234.5], then I use the add operator like this. OP.append = Centroid

Now, for each iteration, I compute the Centroid variable and want to add it to the OP list so that I can use the full list later. But I get this error.

Traceback (last call was last): File "/Users/ahsanakhtar/Documents/TrackingReadyMate/people-counting-opencv/people_counter.py", line 267, in OP.append = centroid AttributeError: list of object attributes, read-only addition

0
source

Source: https://habr.com/ru/post/1397486/


All Articles