使用dbm.ndbm / Berkeley DB在似乎仅安装dbm.dumb的计算机上打开序列化的Python搁板

问题描述

我正在使用一个生成序列化Python货架的软件包。

生成架子的远程计算机上,我可以打开它们并对其进行完美处理。 但是,当我将它们复制到本地计算机上时,将无法再打开它们。

我将问题追溯到dbm子模块(https://docs.python.org/3.1/library/dbm.html)。 在远程上,将dbm.whichdb()搁置(格式:data.db)时,输出为“ dbm.ndbm”,因此似乎已安装了ndbm,我认为第三方Oracle Berkeley很可能而是使用DB,我从dbm库的 init .py文件的源代码中读取了该文件(因为数据格式是.db而不是.pag,.dir):

def whichdb(filename):
    """Guess which db package to use to open a db file.

    Return values:

    - None if the database file can't be read;
    - empty string if the file can be read but can't be recognized
    - the name of the dbm submodule (e.g. "ndbm" or "gnu") if recognized.

    Importing the given module may still fail,and opening the
    database using that module may still fail.
    """

    # Check for ndbm first -- this has a .pag and a .dir file
    try:
        f = io.open(filename + ".pag","rb")
        f.close()
        f = io.open(filename + ".dir","rb")
        f.close()
        return "dbm.ndbm"
    except OSError:
        # some dbm emulations based on Berkeley DB generate a .db file
        # some do not,but they should be caught by the bsd checks
        try:
            f = io.open(filename + ".db","rb")
            f.close()
            # guarantee we can actually open the file using dbm
            # kind of overkill,but since we are dealing with emulations
            # it seems like a prudent step
            if ndbm is not None:
                d = ndbm.open(filename)
                d.close()
                return "dbm.ndbm"
        except OSError:
            pass
...

在我的本地计算机上,运行相同的代码生成文件,data.bak,data.dat和data.dir的三元组。在它们上调用dbm.whichdb()会产生“ dbm.dumb”。 在从远程复制的文件上强制使用dbm.whichdb()会产生“ None”,这意味着根据文档说明数据库是不可读的或损坏的。

我怀疑我缺少打开这些数据库功能

在dbm库中,dumb.py文件充满了内容,但是ndbm.py只说

"""Provide the _dbm module as a dbm submodule."""

from _dbm import *

我认为应该还可以使用ndbm子模块。

如何打开这些ndbm / Berkeley DB数据库

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)