【问题标题】:How to supress b'' character when accessing HDF5 file in Python?在 Python 中访问 HDF5 文件时如何抑制 b'' 字符?
【发布时间】:2021-08-18 03:44:48
【问题描述】:

所以我试图访问 HDF5 文件的子目录中的条目。下面是我的代码

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

如您所见,我使用replace 来获取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''``` 字符?

我已经尝试使用rb 选项访问它。还是一样。

【问题讨论】:

  • 如果您可以将其转换为可重现的示例,那就太好了。现在我们只能猜测entry 是一个组还是一个数据集。也许您可以添加一些生成文件的行,然后使用这些行来读取它。否则这里的 cmets 仍然有点模糊和不确定

标签: python python-3.x hdf5


【解决方案1】:

认为问题在于您实际上返回了一个bytes 对象(如果不使用您的数据复制它,我无法确定)。问题是您不想使用str() 来解码字节,因为这就是首先给您“b”的原因。而是使用entry.decode()bytes 转换为str

【讨论】:

    猜你喜欢
    • 2020-11-15
    • 2015-03-26
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 2019-02-04
    • 2017-03-05
    • 2013-04-10
    相关资源
    最近更新 更多