Python: “List.append = ‘list’ object attribute ‘append’ is read-only”

37,980

Solution 1

>>> 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]

Solution 2

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’]))
Share:
37,980
Chris Chalmers
Author by

Chris Chalmers

Updated on July 13, 2022

Comments

  • Chris Chalmers
    Chris Chalmers almost 2 years

    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?

  • Chris Chalmers
    Chris Chalmers about 12 years
    Thanks very much, really appreciate that! Works perfect but I’m not sure this is the right approach ...I’d like to be able to select specific headings form the Solr response. For example select ‘categories’ specifically from the list and print them to the csv file. To achieve something like this would i have to use a method like Dictionaries? Once again thanks for your help!
  • Useless
    Useless about 12 years
    You mean you want to choose a key ('status', or 'categories'), and extract that key from each result (so you get a list of all the statuses, or all the categories)?
  • Chris Chalmers
    Chris Chalmers about 12 years
    Exactly! So for example on my Solr server I have data such as manufacturer, model and colour. If i wanted say manufacturer and model in the csv I’d print them and their results would be say ‘ford, Vauxhall ... etc etc. So would I be better using a method such as dictionaries?
  • Useless
    Useless about 12 years
    This is getting more complex than really works in comments - time for another question, I think.