python - 低级 h5py h5f 错误:预期字节,找到 str
问题描述
我正在尝试使用修改后的缓存设置创建一个 hdf5 文件处理程序,如下所示:
import h5py
import contextlib
def hdf5_handler(filename, mode="r"):
h5py.File(filename, "a").close()
propfaid = h5py.h5p.create(h5py.h5p.FILE_ACCESS)
settings = list(propfaid.get_cache())
settings[1] = 0
settings[2] = 0
propfaid.set_cache(*settings)
with contextlib.closing(h5py.h5f.open(filename, fapl=propfaid)) as fid:
return h5py.File(fid, mode)
#############
hdf5 = hdf5_handler("/tmp/foo.hdf5", "a")
但它给出了以下错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-121-35fa9f73a406> in <module>()
99 return h5py.File(fid, mode)
100 #############
--> 101 hdf5 = hdf5_handler("/tmp/foo.hdf5", "a")
<ipython-input-121-35fa9f73a406> in hdf5_handler(filename, mode)
96 settings[2] = 0
97 propfaid.set_cache(*settings)
---> 98 with contextlib.closing(h5py.h5f.open(filename, fapl=propfaid)) as fid:
99 return h5py.File(fid, mode)
100 #############
h5py/_objects.pyx in h5py._objects.with_phil.wrapper()
h5py/_objects.pyx in h5py._objects.with_phil.wrapper()
h5py/h5f.pyx in h5py.h5f.open()
TypeError: expected bytes, str found
Python 版本:3.5.5 h5py 版本:'2.8.0'
我还在下面找到了类似的代码,但同样的错误也不适用于我: How to set cache settings while using h5py high level interface?
解决方案
之前将您的字符串转换为字节:
hdf5 = hdf5_handler(bytes("/tmp/foo.hdf5",encoding="utf-8"), "a")
推荐阅读
- javascript - PHP - 安全地允许用户保存 html
- bonjour - 我正在尝试为 _universal._sub._ipp.tcp.local 生成 dns-sd 查询,有没有办法请求子查询
- elasticsearch - Elasticsearch 中的请求时间和搜索延迟持续飙升
- angular - 为什么我们在有打字稿的时候选择角度?
- php - 使用 slack 的 laravel 通知格式不正确
- google-chrome - 如何将扩展程序带到 google chrome 应用程序页面?
- python - Numpy 数组到张量
- asp.net-mvc - 我有用于向他们发送邮件的用户列表。我想在另一个列表框中显示选定的用户。我怎样才能做到这一点?
- javascript - 如何以正确的方式使用抽屉反应原生?
- java - 如何使用 selenium java 识别 chrome 中正在进行的下载?