In Python 3, how to append a nested dictionary to a shelve file with the added difficulty of using a for loop?

Peter Otten __peter__ at web.de
Tue Dec 22 06:45:59 EST 2015


Aaron Christensen wrote:

> Thanks for the response!  Several things you stated definitely got me
> thinking.  I really appreciate the response.  I used what you said and I
> am able to accomplish what I needed.

Perhaps it becomes clearer when you write two helper functions

def read_record(key):
    return db[key]

def write_record(key, value):
    db[key] = value

Changing a person's data then becomes

person_data = read_record("person_1")
person_data["age"] = 123
write_record("person_1", person_data)

Also, I was mostly paraphrasing

>>> help(shelve)

so you might read that, too.




More information about the Python-list mailing list