简体   繁体   English

在Python 3中加载Python 2 .npy文件

[英]Load Python 2 .npy file in Python 3

I'm trying to load /usr/share/matplotlib/sample_data/goog.npy : 我正在尝试加载/usr/share/matplotlib/sample_data/goog.npy

datafile = matplotlib.cbook.get_sample_data('goog.npy', asfileobj=False)
np.load(datafile)

It's fine in Python 2.7, but raises an exception in Python 3.4: 它在Python 2.7中很好,但在Python 3.4中引发了异常:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xd4 in position 1: ordinal not in range(128)

I assume it has something to do with bytes/str/unicode inconsistency between Python 2 and 3, but have no idea how to get through. 我假设它与Python 2和3之间的bytes/str/unicode不一致有关,但不知道如何通过。

Question: 题:

  • How to load a .npy file (NumPy data) from Python 2 in Python 3? 如何在Python 3中从Python 2加载.npy文件(NumPy数据)?

The problem is that the file contains serialized (pickled) Python datetime objects, and not just numerical data. 问题是该文件包含序列化(pickled)Python日期时间对象,而不仅仅是数字数据。 The Python serialization format for these objects is not compatible across Py2 to Py3: 这些对象的Python序列化格式在Py2和Py3之间不兼容:

python2
>>> import pickle
>>> pickle.dumps(datetime.datetime.now())
"cdatetime\ndatetime\np0\n(S'\\x07\\xde\\x06\\t\\x0c\\r\\x19\\x0f\\x1fP'\np1\ntp2\nRp3\n."

and

python3
>>> import pickle
>>> pickle.loads(b"cdatetime\ndatetime\np0\n(S'\\x07\\xde\\x06\\t\\x0c\\r\\x19\\x0f\x1fP'\np1\ntp2\nRp3\n.")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xde in position 1: ordinal not in range(128)

A workaround is to change inside Numpy code 解决方法是更改​​Numpy代码

numpy/lib/format.py:
...
446         array = pickle.load(fp)

to array = pickle.load(fp, encoding="bytes") . to array = pickle.load(fp, encoding="bytes") A better solution would be to allow numpy.load pass on the encoding parameter. 更好的解决方案是允许numpy.load传递编码参数。

In python 3.5 with numpy 1.10.4, using the following command works for me ; 在使用numpy 1.10.4的python 3.5中,使用以下命令对我有用;

D = np.load(file, encoding = 'latin1')

It fails with the same error message when I don't specify the encoding. 当我没有指定编码时,它失败并显示相同的错误消息。

One workaround which helped me is to dump the numpy array loaded in python2.* to a csv file and then read it back in python3.* 帮助我的一个解决方法是将python2。*中加载的numpy数组转储到csv文件中,然后在python3中读取它。*

# Dump in python2
import numpy as np

datafile = matplotlib.cbook.get_sample_data('goog.npy', asfileobj=False)
arr = np.load(datafile)
np.savetxt("np_arr.csv", arr, delimiter=",")

Now read the file back in python3 现在在python3中读回文件

# Load in python3
import numpy as np
arr = np.loadtxt(open("np_arr.csv"), delimiter=",")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM