2015-09-18 31 views
2

我正在努力將代碼從現有的,正在運行的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產生異常並且未能創建數據數組。

顯然我做錯了什麼。有人能幫助我嗎?

+0

我現在沒有h5py,因此恐怕我無法測試您的腳本。如果首先創建沒有數據的數據集,然後填充它,會發生什麼? 'dset = h5.create_dataset('test',shape = z.shape,dtype = z.dtype); dset [:] = z' –

+0

有趣。這將崩潰移動到'dset [:] = z'調用。然而,同樣的症狀仍然存在:如果我沒有在numpy數組上設置dtype,那麼一切都很好。 也許我正確設置了dtype數組。有時間做更多的閱讀。 –

+0

我試過你的腳本:它完美的工作在我的工作站上:Windows 7 64位,h5py 2.5.0,numpy 1.9.2,Python 3.4.3,發行版WinPython-64bit-3.4.3.5。 –

回答

0

原因是由於Python 3.4中的Windows 64位Anaconda 2.3.0中的問題導致的,原因是根據https://github.com/h5py/h5py/issues/593

其他發行版(如WinPython或Anaconda的其他發行版)似乎工作正常。