1

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)'

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

5
  • 1
    Please don't post images of your code or output. Paste the code & output directly, so it's much easier to work with. Commented Jan 4, 2017 at 23:26
  • Sorry I hope this makes it better @RushyPanchal Commented Jan 4, 2017 at 23:34
  • Sorry I hope this makes it better @RadLexus Commented Jan 4, 2017 at 23:37
  • 1
    Try putting and r in front of your file name; r"C:\User. Commented Jan 4, 2017 at 23:39
  • 1
    Avoid backslashes in paths like the plague. Python will happily convert forward-slashes (/) to backslashes on Windows for you. Commented Jan 4, 2017 at 23:46

2 Answers 2

3

Your string:

path = "C:\Users\312001\m2020\data\20170104_145626\doPoint_20170104_150016\dataset_XMIT    data_20170104_150020.h5"

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).

Your options:

  1. Use a raw string by prefixing the string literal with r
  2. Don't use backslashes for paths. Python will convert forward slashes to backslashes for Windows paths.
  3. Double-backslash.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that got rid UnicodeDecodeError and some of the other lines that the terminal was displaying. Ill edit it so that you can see what it shows now. I am still unable to open the file.
0

The answer that @NickT posted fixed the orginal problem I had. The problem that is shown in the new version is due to the hd5 folder names in the hd5 file not matching the folder names that the code provided.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.