1. 程式人生 > >dbm.error: need 'c' or 'n' flag to open new db

dbm.error: need 'c' or 'n' flag to open new db

port utf clas err dict error: class erro lis

#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]

dbm.error: need 'c' or 'n' flag to open new db