來源自 這裡
Preface :
anydbm is a generic interface to variants of the DBM database — dbhash (requires bsddb), gdbm, or dbm. If none of these modules is installed, the slow-but-simple implementation in module dumbdbm will be used. The anydbm module has been renamed to dbm in Python 3. The 2to3 tool will automatically adapt imports when converting your sources to Python 3
Supported APIs :
- anydbm.open(filename[, flag[, mode]])
- exception anydbm.error
Usage Example :
The object returned by open() supports most of the same functionality as dictionaries; keys and their corresponding values can be stored, retrieved, and deleted, and the has_key() and keys() methods are available. Keys and values must always be strings.
The following example records some hostnames and a corresponding title, and then prints out the contents of the database :
Execution result :
Preface :
anydbm is a generic interface to variants of the DBM database — dbhash (requires bsddb), gdbm, or dbm. If none of these modules is installed, the slow-but-simple implementation in module dumbdbm will be used. The anydbm module has been renamed to dbm in Python 3. The 2to3 tool will automatically adapt imports when converting your sources to Python 3
Supported APIs :
- anydbm.open(filename[, flag[, mode]])
- exception anydbm.error
Usage Example :
The object returned by open() supports most of the same functionality as dictionaries; keys and their corresponding values can be stored, retrieved, and deleted, and the has_key() and keys() methods are available. Keys and values must always be strings.
The following example records some hostnames and a corresponding title, and then prints out the contents of the database :
- import dbm
- # Open database, creating it if necessary.
- db = dbm.open('cache', 'c')
- # Record some values
- db['www.python.org'] = 'Python Website'
- db['www.cnn.com'] = 'Cable News Network'
- # Loop through contents. Other dictionary methods
- # such as .keys(), .values() also work.
- print("\t[Info] Show db :")
- for k, v in db.items():
- print("\t[Info] Key={0} ; Value={1}...".format(k, v))
- # Storing a non-string key or value will raise an exception (most
- # likely a TypeError).
- #db['www.yahoo.com'] = 4 <--- Will have exception : TypeError: values must be bytes or strings
- # Close when done.
- db.close()
- db2 = dbm.open('cache')
- print("\t[Info] Show db2 restored from file :")
- for k, v in db2.items():
- print("\t[Info] Key={0} ; Value={1}...".format(k, v))
This message was edited 6 times. Last update was at 28/05/2012 10:35:10
沒有留言:
張貼留言