我只能从julia到HDF5读写字符串,而不会出现问题:
julia> h5write("test.h5", "stringdset", "some string")
0
julia> h5read("test.h5", "stringdset")
"some string"
由h5py编写的字符串可由Python和Julia读取:
In [27]: with h5py.File("testpy.h5", "a") as f:
...: f["testdset"] = "works from python"
In [29]: with h5py.File("testpy.h5", "a") as f:
...: print(f["testdset"][()])
works from python
julia> h5read("testpy.h5", "testdset")
"works from python"
但是,当我从Julia编写一个字符串时,h5py找不到要读取的转换:
with h5py.File("test.h5") as f:
...: f["stringdset"][()]
...:
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-6-cc416a9c995d> in <module>
1 with h5py.File("test.h5") as f:
----> 2 f["stringdset"][()]
3
h5py/_objects.pyx in h5py._objects.with_phil.wrapper()
h5py/_objects.pyx in h5py._objects.with_phil.wrapper()
~/envs/data/lib/python3.7/site-packages/h5py/_hl/dataset.py in __getitem__(self, args)
544 arr = numpy.ndarray(selection.mshape, dtype=new_dtype)
545 for mspace, fspace in selection:
--> 546 self.id.read(mspace, fspace, arr, mtype)
547 if len(names) == 1:
548 arr = arr[names[0]]
h5py/_objects.pyx in h5py._objects.with_phil.wrapper()
h5py/_objects.pyx in h5py._objects.with_phil.wrapper()
h5py/h5d.pyx in h5py.h5d.DatasetID.read()
h5py/_proxy.pyx in h5py._proxy.dset_rw()
h5py/_proxy.pyx in h5py._proxy.H5PY_H5Dread()
OSError: Can't read data (no appropriate function for conversion path)
这可能与可变长度字符串有关? h5py可以识别dtype,但仍无法读入。
In [7]: f = h5py.File("test.h5")
In [13]: f["stringdset"]
Out[13]: <HDF5 dataset "stringdset": shape (), type "|S11">
In [8]: f["stringdset"].dtype
Out[8]: dtype('S11')
我错过了一些简单的解决方法吗?还是保守的h5py正在使用字符串编码?
仅供参考,这是我的版本信息:
In [3]: print(h5py.version.info)
Summary of the h5py configuration
---------------------------------
h5py 2.9.0
HDF5 1.10.4
Python 3.7.3 (default, Jun 19 2019, 07:38:49)
[Clang 10.0.1 (clang-1001.0.46.4)]
sys.platform darwin
sys.maxsize 9223372036854775807
numpy 1.16.2