Python 低级别h5py h5f错误:需要字节,找到str
我正在尝试使用修改的缓存设置创建hdf5文件处理程序,如下所示:Python 低级别h5py h5f错误:需要字节,找到str,python,hdf5,h5py,low-level,Python,Hdf5,H5py,Low Level,我正在尝试使用修改的缓存设置创建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]
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
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
99返回h5py.文件(fid,模式)
100 #############
-->101 hdf5=hdf5\u处理程序(“/tmp/foo.hdf5”,“a”)
在hdf5_处理程序中(文件名,模式)
96设置[2]=0
97 propfaid.set_缓存(*设置)
--->98将contextlib.closing(h5py.h5f.open(filename,fapl=propfaid))作为fid:
99返回h5py.文件(fid,模式)
100 #############
h5py.\u objects.with\u phil.wrapper()中的h5py/\u objects.pyx
h5py.\u objects.with\u phil.wrapper()中的h5py/\u objects.pyx
h5py.h5f.open()中的h5py/h5f.pyx
TypeError:需要字节,找到str
Python版本:3.5.5
h5py版本:“2.8.0”
我还在以下位置发现了类似的代码,但同样的错误也不适用于我:
在以下操作之前将字符串转换为字节:
hdf5 = hdf5_handler(bytes("/tmp/foo.hdf5",encoding="utf-8"), "a")
请尝试:
hdf5=hdf5\u处理程序(字节(“/tmp/foo.hdf5”,encoding=“utf-8”),“a”)
是的,谢谢,这样可以工作。发生了什么变化?为什么在前一篇文章中他们没有使用字节?我不知道。我认为他们正在使用其他python版本。