【发布时间】:2015-09-21 22:23:28
【问题描述】:
我正在努力将代码从现有的、正在运行的 Python 2.7 应用程序迁移到 Python 3。它在 Windows 7 上运行,并使用 Anaconda 发行版。
在该代码中,我使用 H5Py 和 NumPy 生成 HDF5 文件。该代码在 Python 2.7 中运行良好,但在 Python 3.4 (H5Py 2.5) 中,h5.create_dataset() 上的代码段错误。
以下是我的最低限度的复制测试用例:
import h5py
import numpy
import tempfile
data = [(1,2,3) for x in range(100)]
dtypes=[('one',numpy.dtype(int),),('two',numpy.dtype(int),), ('three',numpy.dtype(int),)]
tmp = tempfile.NamedTemporaryFile(delete=False)
filename = tmp.name
h5 = None
try:
with h5py.File(filename,'w') as h5:
print('Creating NumPY array')
z = numpy.array(data, dtype=dtypes)
print('Creating dataset')
dset = h5.create_dataset('test', data=z) #python crashes here.
except Exception as e:
print(e)
finally:
if h5:
h5.close()
print('Python never reaches here.')
如果我为 NumPy 数组丢弃 dtypes,则代码有效。但我需要dtype 设置(在我的完整应用程序中,这个数组是根据手头的数据动态生成的)。
即,如果我改变
z = numpy.array(data, dtype=dtypes)
到
z = numpy.array(data)
代码有效。
我尝试以各种方式对 dtype 类型名称(即“一”、“二”、“三”)进行编码,认为 Py3 字符串中的 unicode 可能是问题所在。相反,NumPy 产生了异常并且未能创建数据数组。
显然我做错了什么。有人可以帮我吗?
【问题讨论】:
-
我现在没有 h5py,所以恐怕我无法测试你的脚本。如果您首先创建没有数据的数据集,然后填充它会发生什么?
dset = h5.create_dataset('test', shape=z.shape, dtype=z.dtype); dset[:] = z -
有趣。这会将崩溃转移到
dset[:] = z调用。但是,同样的症状仍然存在:如果我不在 numpy 数组上设置 dtype,一切都很好。也许我错误地设置了 dtype 数组。是时候做更多的阅读了。 -
我试过你的脚本:它在我的工作站上完美运行:Windows 7 64 位,h5py 2.5.0,numpy 1.9.2,Python 3.4.3,发行版 WinPython-64bit-3.4.3.5 .
-
我使用 WinPython 观察到同样的情况。这使我陷入了兔子洞this github issue。看起来像 H5Py 的官方 Anaconda 发行版目前已被破坏。桃色。感谢您对此的帮助!
标签: python python-3.x python-3.4 h5py