Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

python - How to set value in shelve

I want to store dictionary in shelve and also want to put data in that. How can i do that?

import shelve

s = shelve.open("test")
s['flag'] = {}
question from:https://stackoverflow.com/questions/65883694/how-to-set-value-in-shelve

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You cannot edit values directly, so you have to update a copy and then reset it:

import shelve

s = shelve.open("test")
s['flag'] = {}
temp = s['flag']
temp['foo'] = 'bar'
s['flag'] = temp

you cannot do

s['flag']['foo'] = 'bar'

directly because s['flag'] returns a copy


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...