#coding=utf-8
import shelve
with shelve.open("shelve.ini","w") as f:
    f["k1"] = test_list
    f["k2"] = test_dict
    f["k3"] = s
 
with shelve.open("shelve.ini","r") as f:
    print(f["k3"])
    print(f["k2"])
    print(f["k1"])

报错

    raise error[0]("need 'c' or 'n' flag to open new db")
dbm.error: need 'c' or 'n' flag to open new db

 

改为如下,加入c参数:

#coding=utf-8
import shelve
test_list=[1,2,3,4,5]
test_dict={"aaa":23,"bbb":33}
s="xiaoming"
with shelve.open("shelve.ini","wc") as f:
    f["k1"] = test_list
    f["k2"] = test_dict
    f["k3"] = s
 
with shelve.open("shelve.ini","rc") as k:
    print(k["k3"])
    print(k["k2"])
    print(k["k1"])

输出

xiaoming
{'aaa': 23, 'bbb': 33}
[1, 2, 3, 4, 5]

 

相关文章:

  • 2022-01-13
  • 2022-12-23
  • 2022-02-21
  • 2021-12-24
  • 2021-10-14
  • 2021-06-14
  • 2021-09-06
猜你喜欢
  • 2022-12-23
  • 2021-10-21
  • 2021-12-30
  • 2022-12-23
  • 2022-12-23
  • 2022-01-15
相关资源
相似解决方案