如何使用dbm
# -*- coding:utf-8 -*-
import dbm
db = dbm.open("definitions", "c")
db["mustard"] = "yellow"
db["ketchup"] = "red"
db["pesto"] = "green"
len(db)
print db["pesto"]
db.close()
db = dbm.open("definitions", "r")
db["mustard"]
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-2974085df590> in <module>()
1
----> 2 import dbm
3 db = dbm.open("definitions", "c")
4 db["mustard"] = "yellow"
5 db["ketchup"] = "red"
ImportError: No module named dbm
什么是dbm
dbm格式是按照键值对的形式存储、封装在应用程序中,用来维护各种各样的配置
类似于Python字典,键值对格式,自动保存于磁盘的数据库;通过键查找到对应的值。
键值都以字节保存,不能对数据库对象进行迭代。