我正在尝试打开一个HDF5文件,以便用python读取它,这样我以后可以用它做更多的事情。运行程序读取文件时出错。该计划如下:
import h5py # HDF5 support
import numpy
fileName = "C:/.../file.h5"
f = h5py.File(fileName, "r")
for item in f.attrs.keys():
print item + ":", f.attrs[item]
mr = f['/entry/mr_scan/mr']
i00 = f['/entry/mr_scan/I00']
print "%s\t%s\t%s" % ("#", "mr", "I00")
for i in range(len(mr)):
print "%d\t%g\t%d" % (i, mr[i], i00[i])
f.close()
如果我运行该程序,我最终会看到此错误:
Traceback (most recent call last):
File "TestHD5.py", line 8, in <module>
mr = f['/entry/mr_scan/mr']
File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (C:\aroot\work\h5py\_objects.c:2587)
File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper (C:\aroot\work\h5py\_objects.c:2546)
File "C:\programs\Python27\lib\site-packages\h5py\_hl\group.py", line 166, in __getitem__
oid = h5o.open(self.id, self._e(name), lapl=self._lapl)
File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (C:\aroot\work\h5py\_objects.c:2587)
File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper (C:\aroot\work\h5py\_objects.c:2546)
File "h5py\h5o.pyx", line 190, in h5py.h5o.open (C:\aroot\work\h5py\h5o.c:3417)
KeyError: 'Unable to open object (Component not found)'
我只是缺少一些模块来读取文件,或者这是其他的东西。如果我使用h5文件veiwer程序,它将打开.h5文件。谢谢
答案 0 :(得分:2)
你的字符串:
path = "C:\Users\312001\m2020\data\20170104_145626\doPoint_20170104_150016\dataset_XMIT data_20170104_150020.h5"
充满了破碎/非法escapes(幸运的是,他们将变成SyntaxErrors,虽然你使用的是Python 2),还有一些做的工作,所以Python认为{{ 1}}实际上等于:path
(请注意那些'C:\\Users\xca001\\m2020\\data\x8170104_145626\\doPoint_20170104_150016\\dataset_XMIT data_20170104_150020.h5'
&#39; s。
您的选择:
\x##
答案 1 :(得分:0)
@NickT发布的答案解决了我遇到的原始问题。新版本中显示的问题是由于hd5文件中的hd5文件夹名称与代码提供的文件夹名称不匹配。