Python无法打开.h5文件 [英] Python unable to open a .h5 file
问题描述
我正在尝试打开HDF5文件以便使用python读取它,以便以后可以使用它做更多的事情.运行程序以读取文件时出现错误.该程序如下:
I am trying to open a HDF5 file in order to read it with python, so that I can do more things with it later. There is an error when I run the program to read the file. The program is below:
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()
如果我运行程序,最终会看到此错误:
If I run the program I end up seeing this error:
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文件.谢谢
Am I just missing some modules to read the file, or is this something else. It will open the .h5 file if I use an h5 file veiwer program. Thank you
推荐答案
您的字符串:
path = "C:\Users\312001\m2020\data\20170104_145626\doPoint_20170104_150016\dataset_XMIT data_20170104_150020.h5"
到处都是破损/非法的转义(非常感谢尽管您使用的是Python 2,但它们会变成SyntaxErrors,而有些实际上是有效的,因此Python认为 path
确实等于:'C:\\ Users \ xca001 \\ m2020 \\ data \ x8170104_145626 \\ doPoint_20170104_150016 \\ dataset_XMIT data_20170104_150020.h5'
(请注意那些 \ x ##
's).
is full of broken/illegal escapes (thankfully they will be turned into SyntaxErrors, though you are using Python 2), and some that actually do work, so Python thinks path
is really equal to: 'C:\\Users\xca001\\m2020\\data\x8170104_145626\\doPoint_20170104_150016\\dataset_XMIT data_20170104_150020.h5'
(note those \x##
's).
您的选择:
- 使用 原始字符串,方法是在字符串文字前加上
r
- 请勿在路径中使用反斜杠.Python将Windows路径的正斜杠转换为反斜杠.
- 双反斜杠.
这篇关于Python无法打开.h5文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!