[英]Convert Python shelve from dbm.gnu to dbm.dumb
I am trying to convert data stored in a non-dumb shelve to a dumb shelve, to be able to access my data at a location where the non-dumb libraries are not installed.我正在尝试将存储在非哑搁架中的数据转换为哑搁置,以便能够在未安装非哑库的位置访问我的数据。
My test code for transforming the db data looks like this:我用于转换数据库数据的测试代码如下所示:
import numpy as np
import shelve
import dbm
# create test shelve
sample = list(range(0, 10))
filename = 'test'
new_filename = 'test-dumb'
with shelve.open(filename) as data:
data['sample'] = sample
print('current db type: ', dbm.whichdb(filename))
# test test shelve
with shelve.open(filename) as data:
print('in current db: ', list(data.keys()))
# read test shelve
with shelve.open(filename) as data:
# store in dumb format
with dbm.dumb.open(new_filename) as dumb_data:
dumb_data = data
print('in new db: ', list(dumb_data.keys()))
print('\nnew db type: ', dbm.whichdb(new_filename))
# check dumb shelve
with dbm.dumb.open(new_filename) as dumb_data:
print(list(dumb_data.keys()))
if dumb_data['sample'] == sample:
print('success: ', sample)
else:
print('not yet: ', sample)
The output is the following: output 如下:
current db type: dbm.gnu
in current db: ['sample']
in new db: ['sample']
new db type: dbm.dumb
[]
Traceback (most recent call last):
File "/home/asdf/anaconda3/envs/phievo/lib/python3.7/dbm/dumb.py", line 153, in __getitem__
pos, siz = self._index[key] # may raise KeyError
KeyError: b'sample'
What am I doing wrong here?我在这里做错了什么?
You need to iterate over the keys of the original database, assigning with:您需要遍历原始数据库的键,分配:
dumb_data = data
Will just override dumb_data
variable with a "pointer" to data
.只会用指向data
的“指针”覆盖dumb_data
变量。
The program should look something like the following:该程序应如下所示:
import numpy as np
import shelve
import dbm
...
# copy data into dumb
with shelve.open(filename) as data:
# store in dumb format
with dbm.dumb.open(new_filename) as dumb_data:
for key, value in data.items():
dumb_data[key] = value
print('in new db: ', list(dumb_data.keys()))
I just replaced the assignation with:我只是将作业替换为:
for key, value in data.items():
dump_data[key] = value`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.