import shelve
包
- 保存
def ValueSave(*args):
with shelve.open('pythonVal.dat') as f:
for i,val in enumerate(args):
try:
f[str(i)]=val
except BaseException:pass
- 加载
def ValueLoad():
ans=[]
with shelve.open('pythonVal.dat') as f:
size=len(f)
for i in range(size):
ans.append(f[str(i)])
return ans
- 测试
def learn_value_IO():
a=(6,'123',(2,3))
b=[1,2,3]
c=dict(a=333)
ValueSave(a,b,c)
r1,r2,r3=ValueLoad()
print(r1)
print(r2)
print(r3)
得到结果:
(6, '123', (2, 3))
[1, 2, 3]
{'a': 333}