我正在努力將代碼從現有的,正在運行的Python 2.7應用程序遷移到Python 3.這是在Windows 7上運行並使用Anaconda發行版。通過H5Py創建HDF5文件時Python3崩潰
在那段代碼中,我使用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.')
如果我放棄dtypes
,爲NumPy的數組,然後代碼工作。但我需要dtype
設置(在我的完整應用程序中,此數組根據手頭數據動態生成)。
即,如果我改變
z = numpy.array(data, dtype=dtypes)
到
z = numpy.array(data)
代碼工作。
我試圖用各種方式編碼dtype
類型名稱(即'one','two','three'),可能是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。 –