python保存加载本地变量

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}

这里写图片描述

猜你喜欢

转载自blog.csdn.net/tqcai666/article/details/80202047
今日推荐