Search code examples
pythonpython-3.xhdf5

How to supress b'' character when accessing HDF5 file in Python?


So I'm trying to access entries in subdirectories in a HDF5 file. Below is my code

data = h5py.File(filename, 'r')

    seqTags = []
    for entry in data['somesebdir']:
        seqTags.append(str(entry).replace("b'", "")[:-1])
        seq_list_file.append(str(entry).replace("b'", "")[:-1])

As you can see I'm using replace to get with of b'something'` to get something. However I would like to avoid doing this. How do I just get the content within HDF5 file without having to worry about this b''``` character?

I've already tried accessing it with rb option. It was still the same.


Solution

  • I think the problem is that you actually have a bytes object being returned (I can't be sure without reproducing it with your data). The problem is you don't want to use str() to decode the bytes, because that's what's giving you the 'b' in the first place. Instead use entry.decode() to convert the bytes to a str.