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

Posted F

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dbm.error: need 'c' or 'n' flag to open new db相关的知识,希望对你有一定的参考价值。

 

 

#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的主要内容,如果未能解决你的问题,请参考以下文章

python 的 chromedriver' executable needs to be in PATH.

Message: 'chromedriver' executable needs to be in PATH.解决办法(综合了网上的一些办法)

在运行QT中出现 error: No rule to make target `c:/Documents', needed by `debug/Documents.o'

'chromedriver' executable needs to be in PATH

解决 'chromedriver' executable needs to be in PATH.'报错

使U_boot支持DM9000网卡芯片;编译出错:No rule to make target'dm9000.c'.need by'.depend'.stop