发布网友 发布时间:2022-05-01 19:35
共2个回答
懂视网 时间:2022-04-11 08:18
dbm.gnu
dbm.ndbm
dbm.dumb
1、dbm模块创建一个简单的数据库
#!/usr/bin/env python3 # encoding: utf-8 import dbm with dbm.open(‘example.db‘, ‘n‘) as db: db[‘key‘] = ‘value‘ db[‘today‘] = ‘Monday‘ db[‘author‘] = ‘suk‘dbm_new.py
运行效果
[root@python-mysql mnt]# python3 dbm_new.py [root@python-mysql mnt]# ll -rw-r--r-- 1 root root 171 Jan 12 16:18 dbm_new.py -rw-r--r-- 1 root root 52 Jan 12 16:19 example.db.bak -rw-r--r-- 1 root root 1027 Jan 12 16:19 example.db.dat -rw-r--r-- 1 root root 52 Jan 12 16:19 example.db.dir
2、dbm模块查看数据存储格式
#!/usr/bin/env python3 # encoding: utf-8 import dbm print(dbm.whichdb(‘example.db‘))dbm_whichdb.py
运行效果
[root@python-mysql mnt]# python3 dbm_whichdb.py dbm.dumb
3、dbm模块读取数据库
#!/usr/bin/env python3 # encoding: utf-8 import dbm with dbm.open(‘example.db‘, ‘r‘) as db: print(‘keys()‘, db.keys()) for k in db.keys(): print(‘迭代数据:‘, k, db[k]) print("db[‘author‘]", db[‘author‘])dbm_read.py
运行效果
[root@python-mysql mnt]# python3 dbm_read.py keys() [b‘key‘, b‘today‘, b‘author‘] 迭代数据: b‘key‘ b‘value‘ 迭代数据: b‘today‘ b‘Monday‘ 迭代数据: b‘author‘ b‘suk‘ db[‘author‘] b‘suk‘
4、dbm模块设置值key必须是字符串或字节类型
#!/usr/bin/env python3 # encoding: utf-8 import dbm with dbm.open(‘example.db‘, ‘w‘) as db: try: db[1] = ‘one‘ except TypeError as err: print(err)dbm_intkeys.py
运行效果
[root@python-mysql mnt]# python3 dbm_intkeys.py keys must be bytes or strings
5、dbm模块设置值value必须是字符串或字节类型
#!/usr/bin/env python3 # encoding: utf-8 import dbm with dbm.open(‘example.db‘, ‘w‘) as db: try: db[‘one‘] =1 except TypeError as err: print(err)dbm_intvalue.py
运行效果
[root@python-mysql mnt]# python3 dbm_intvalue.py values must be bytes or strings
Python之dbm模块的使用
标签:byte bytes -- hide port hid lap python3 event
热心网友 时间:2022-04-11 05:26
如果你想通过函数的名称来获取函数的运行地址,可以像下面这样实现:
[python] view plain copy
# File: builtin-import-example-2.py
def getfunctionbyname(mole_name, function_name):
mole = __import__(mole_name)
return getattr(mole, function_name)
print( repr(getfunctionbyname("dbm", "open")) )
运行之后,输出如下:
=== RESTART: D:\work\csdn\python_Game1\example\builtin-import-example-2.py ===
<function open at 0x00000226467B2BF8>
>>>