首页 > 解决方案 > 如何在搁置中设置价值

问题描述

我想将字典存储在搁置中,也想将数据放入其中。我怎样才能做到这一点?

import shelve

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

标签: pythonpython-3.xshelve

解决方案


您不能直接编辑值,因此您必须更新副本然后重置它:

import shelve

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

你不能做

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

直接因为s['flag']返回一个副本


推荐阅读