本文介绍了在 python 3 中使用 python 2 架子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将数据存储在使用 python 2.7 创建的架子文件中

当我尝试从 python 3.4 访问文件时,出现错误:

>>>进口货架>>>人口=shelve.open('shelve.shelf')回溯(最近一次调用最后一次):文件<stdin>",第 1 行,位于 <module>文件C:\Python34\lib\shelve.py",第 239 行,打开返回 DbfilenameShelf(文件名,标志,协议,写回)文件C:\Python34\lib\shelve.py",第 223 行,在 __init__ 中Shelf.__init__(self, dbm.open(filename, flag), protocol, writeback)文件C:\Python34\lib\dbm\__init__.py",第 88 行,打开引发错误[0](无法确定数据库类型")dbm.error: 无法确定数据库类型

我仍然可以在 python 2.7 中毫无问题地访问书架,所以似乎存在向后兼容性问题.有什么办法可以用新的python版本直接访问旧格式吗?

解决方案

据我所知,这是导致我的问题的路径:

  • 原始书架是在 Windows 中使用 Python 2 创建的
  • Python 2 Windows 默认使用 bsddb 作为搁置的底层数据库,因为 dbm 在 Windows 平台上不可用
  • Python 3 不附带 bsddb.底层数据库是 Python 3 for Windows 中的 dubdbm.

我起初考虑为 Python 3 安装第三方 bsddb 模块,但很快就变得很麻烦.然后似乎每当我需要在新机器上使用相同的架子文件时,这将是一个反复出现的麻烦.所以我决定将文件从 bsddb 转换为 dumbdbm,我的 python 2 和 python 3 安装都可以读取.

我在 Python 2 中运行了以下内容,该版本包含 bsddb 和 dumbdbm:

导入搁置导入哑数据库def dubdbm_shelve(文件名,标志=c"):return shelve.Shelf(dumbdbm.open(filename,flag))out_shelf=dumbdbm_shelve("shelved.dumbdbm.shelf")in_shelf=shelve.open("shelved.shelf")key_list=in_shelf.keys()对于 key_list 中的键:out_shelf[key]=in_shelf[key]out_shelf.close()in_shelf.close()

目前看来,dumbdbm.shelf 文件没有问题,等待对内容进行双重检查.

I have data stored in a shelf file created with python 2.7

When I try to access the file from python 3.4, I get an error:

>>> import shelve
>>> population=shelve.open('shelved.shelf')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python34\lib\shelve.py", line 239, in open
    return DbfilenameShelf(filename, flag, protocol, writeback)
  File "C:\Python34\lib\shelve.py", line 223, in __init__
    Shelf.__init__(self, dbm.open(filename, flag), protocol, writeback)
  File "C:\Python34\lib\dbm\__init__.py", line 88, in open
    raise error[0]("db type could not be determined")
dbm.error: db type could not be determined

I'm still able to access the shelf with no problem in python 2.7, so there seems to be a backward-compatibility issue. Is there any way to directly access the old format with the new python version?

解决方案

As I understand now, here is the path that lead to my problem:

  • The original shelf was created with Python 2 in Windows
  • Python 2 Windows defaults to bsddb as the underlying database for shelving, since dbm is not available on the Windows platform
  • Python 3 does not ship with bsddb. The underlying database is dumbdbm in Python 3 for Windows.

I at first looked into installing a third party bsddb module for Python 3, but it quickly started to turn into a hassle. It then seemed that it would be a recurring hassle any time I need to use the same shelf file on a new machine. So I decided to convert the file from bsddb to dumbdbm, which both my python 2 and python 3 installations can read.

I ran the following in Python 2, which is the version that contains both bsddb and dumbdbm:

import shelve
import dumbdbm

def dumbdbm_shelve(filename,flag="c"):
    return shelve.Shelf(dumbdbm.open(filename,flag))

out_shelf=dumbdbm_shelve("shelved.dumbdbm.shelf")
in_shelf=shelve.open("shelved.shelf")

key_list=in_shelf.keys()
for key in key_list:
    out_shelf[key]=in_shelf[key]

out_shelf.close()
in_shelf.close()

So far it looks like the dumbdbm.shelf files came out ok, pending a double-check of the contents.

这篇关于在 python 3 中使用 python 2 架子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 23:37
查看更多