- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我想知道是否有办法使用 Python 3.4 加载在 Python 2.4 中 pickle 的对象。
我一直在对大量公司遗留代码运行 2to3 以使其保持最新状态。
完成此操作后,在运行文件时出现以下错误:
File "H:\fixers - 3.4\addressfixer - 3.4\trunk\lib\address\address_generic.py"
, line 382, in read_ref_files
d = pickle.load(open(mshelffile, 'rb'))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 1: ordinal
not in range(128)
查看竞争中的 pickle 对象,它是 dict
中的 dict
,包含 str
类型的键和值。
所以我的问题是:有没有办法使用 python 3.4 加载最初在 python 2.4 中 pickle 的对象?
最佳答案
您必须告诉 pickle.load()
如何将 Python 字节串数据转换为 Python 3 字符串,或者您可以告诉 pickle
将它们保留为字节。
默认是尝试将所有字符串数据解码为 ASCII,但解码失败。见 pickle.load()
documentation :
Optional keyword arguments are fix_imports, encoding and errors, which are used to control compatibility support for pickle stream generated by Python 2. If fix_imports is true, pickle will try to map the old Python 2 names to the new names used in Python 3. The encoding and errors tell pickle how to decode 8-bit string instances pickled by Python 2; these default to ‘ASCII’ and ‘strict’, respectively. The encoding can be ‘bytes’ to read these 8-bit string instances as bytes objects.
将编码设置为latin1
可以直接导入数据:
with open(mshelffile, 'rb') as f:
d = pickle.load(f, encoding='latin1')
但您需要确认您的所有字符串均未使用错误的编解码器进行解码; Latin-1 适用于任何输入,因为它将字节值 0-255 直接映射到前 256 个 Unicode 代码点。
另一种方法是使用 encoding='bytes'
加载数据,然后解码所有 bytes
键和值。
请注意,直到 3.6.8、3.7.2 和 3.8.0 之前的 Python 版本,unpickling of Python 2 datetime
object data is broken除非你使用 encoding='bytes'
。
关于python - 用 python 3 解开一个 python 2 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28218466/
是否可以在 Delphi 中解开这些名称?如果是这样,我从哪里可以获得更多信息? 在 dbrtl100.bpl 中找不到特定条目的错误消息示例我想知道它找不到哪个确切的函数(单元、类、名称、参数等)。
我是小白,如何从列表中删除引号和逗号?或者我如何“取消字符串”? 在不显示字典 (dic) 的情况下,我使用的代码如下所示: >>>import itertools >>>list(itertools
我正在学习构建器模式,到目前为止我了解到,它是用于初始化的常用模式的一个很好的替代方案: 伸缩构造函数模式 JavaBean 模式 问题是,我真的不喜欢从我的域模型中的对象中删除 getter 和 s
我有这段文字 "Welcome to my city. Hello my good friend" 使用 jQuery(或 javascript),我想解开所有 span 标签,只保留第一级,我的意思
在为大学撰写作业的过程中,我一直享受着学习新的 Haskell monad 的乐趣。耶!!! 我有一个可以很好地进行类型检查的函数: compile :: Prog -> State VarsStat
let myLabel:UILabel = UILabel(frame:CGRectMake(0, 0, 44, 44)) myLabel.text = "Primary"
是否有一个好的 npm 包可以删除在 NodeJS 服务器上运行的 html 字符串中不必要的嵌套标签(无浏览器 DOM)?我尝试过 sanitize-html,但似乎不可能做到这一点。 我收到用户发
我目前正在编写一些日志代码,这些代码应该——除其他外——打印有关调用函数的信息。这应该比较容易,标准C++有一个type_info类(class)。这包含 typeid 的类/函数/等的名称。但它被破
我有以下组件 const ListItem = ({ children, title = '' }) => { const [isActive, setIsActive] = useSta
我正在尝试取消在 python3 中 pickle 的对象。这在 python3 中有效,但在 python2 中无效。该问题可以重现到 pickle 协议(protocol) 0。示例代码: imp
这个问题的灵感来自 How to transform a flow chart into an implementation?它询问从代码中通过算法消除 goto 语句的方法。 answer this
当我的 child 执行 unwind segue 时,我的 Controller 的 viewDidAppear 被调用。 在这个方法中(只有这个方法,我需要知道它是否来自 unwind) 注意:
我用 C 编写了一个小程序来管理 SQLite3 中的书目数据库。到目前为止,这只是一个允许导入和导出 BibTeX 数据的命令行工具。为了使导出/导入功能更加可定制(例如,在导入时始终在日期字段中组
这个问题与How to split an array according to a condition in numpy?密切相关但我正在寻找一种更通用的方法来拆分给定未知数量索引的数组: impor
我是一名优秀的程序员,十分优秀!