使用 unicode 在 hdf5 中存储字符串数据集
Storing string datasets in hdf5 with unicode
我正在尝试从包含特殊字符的文件中存储变量字符串表达式,例如 ø, æ , and å
。这是我的代码:
import h5py as h5
file = h5.File('deleteme.hdf5','a')
dt = h5.special_dtype(vlen=str)
dset = file.create_dataset("text",(1,),dtype=dt)
dset.attrs[str(1)] = "some text with ø, æ, å"
但是文本存储不正确。存储的数据包含文本:
"some text with 777777703777777670, 777777703777777646,777777703777777645"
如何正确存储特殊字符?我已尝试按照此处文档中提供的指南进行操作:Strings in HDF5 - Variable-length UTF-8
编辑:
输出来自 h5dump。下面的答案验证了字符是否正确存储为 utf-8。
您应该尝试通过执行以下操作以 UTF-8 格式存储数据:
要以 utf-8 格式编码(在使用 h5py 存储之前)执行:
u"æ".encode("utf-8")
其中 return 个:
'\xc3\xa6'
然后要解码你可以使用这样的字符串解码:
'\xc3\xa6'.decode("utf-8")
这会 return:
æ
希望对您有所帮助!
编辑
当您打开文件并希望它们是 utf-8 格式时,您可以在读取文件方法中使用编码参数:
f = open(fname, encoding="utf-8")
这应该有助于正确编码原始文件。
来源:python-notes
与:
import numpy as np
import h5py as h5
file = h5.File('deleteme.hdf5','w')
dt = h5.special_dtype(vlen=str)
dset = file.create_dataset("text",(3,),dtype=dt)
dset[:] = 'ø æ å'.split()
dset.attrs["1"] = "some text with ø, æ, å"
file.close()
file = h5.File('deleteme.hdf5','r')
print(file['text'][:])
print(file['text'].attrs["1"])
file.close()
我明白了:
$ python3 stack44661467.py
['ø' 'æ' 'å']
some text with ø, æ, å
即 h5py
将字符串 see/interpret 作为 unicode - 写入和读取。
使用转储实用程序:
$ h5dump deleteme.hdf5
HDF5 "deleteme.hdf5" {
GROUP "/" {
DATASET "text" {
DATATYPE H5T_STRING {
STRSIZE H5T_VARIABLE;
STRPAD H5T_STR_NULLTERM;
CSET H5T_CSET_UTF8;
CTYPE H5T_C_S1;
}
DATASPACE SIMPLE { ( 3 ) / ( 3 ) }
DATA {
(0): "777777703777777670", "777777703777777646",
(2): "777777703777777645"
}
ATTRIBUTE "1" {
DATATYPE H5T_STRING {
STRSIZE H5T_VARIABLE;
STRPAD H5T_STR_NULLTERM;
CSET H5T_CSET_UTF8;
CTYPE H5T_C_S1;
}
DATASPACE SCALAR
DATA {
(0): "some text with 777777703777777670, 777777703777777646, 777777703777777645"
}
}
}
}
}
请注意,在这两种情况下,datatype
都标记为 UTF8
DATATYPE H5T_STRING {
STRSIZE H5T_VARIABLE;
STRPAD H5T_STR_NULLTERM;
CSET H5T_CSET_UTF8;
CTYPE H5T_C_S1;
}
文档是这么说的:
http://docs.h5py.org/en/latest/strings.html#variable-length-utf-8
They can store any character a Python unicode string can store, with the exception of NULLs. In the file these are created as variable-length strings with character set H5T_CSET_UTF8.
让 h5py
(或其他 reader)担心将 777777703777777670
解释为正确的 unicode 字符。
我正在尝试从包含特殊字符的文件中存储变量字符串表达式,例如 ø, æ , and å
。这是我的代码:
import h5py as h5
file = h5.File('deleteme.hdf5','a')
dt = h5.special_dtype(vlen=str)
dset = file.create_dataset("text",(1,),dtype=dt)
dset.attrs[str(1)] = "some text with ø, æ, å"
但是文本存储不正确。存储的数据包含文本:
"some text with 777777703777777670, 777777703777777646,777777703777777645"
如何正确存储特殊字符?我已尝试按照此处文档中提供的指南进行操作:Strings in HDF5 - Variable-length UTF-8
编辑:
输出来自 h5dump。下面的答案验证了字符是否正确存储为 utf-8。
您应该尝试通过执行以下操作以 UTF-8 格式存储数据:
要以 utf-8 格式编码(在使用 h5py 存储之前)执行:
u"æ".encode("utf-8")
其中 return 个:
'\xc3\xa6'
然后要解码你可以使用这样的字符串解码:
'\xc3\xa6'.decode("utf-8")
这会 return:
æ
希望对您有所帮助!
编辑
当您打开文件并希望它们是 utf-8 格式时,您可以在读取文件方法中使用编码参数:
f = open(fname, encoding="utf-8")
这应该有助于正确编码原始文件。
来源:python-notes
与:
import numpy as np
import h5py as h5
file = h5.File('deleteme.hdf5','w')
dt = h5.special_dtype(vlen=str)
dset = file.create_dataset("text",(3,),dtype=dt)
dset[:] = 'ø æ å'.split()
dset.attrs["1"] = "some text with ø, æ, å"
file.close()
file = h5.File('deleteme.hdf5','r')
print(file['text'][:])
print(file['text'].attrs["1"])
file.close()
我明白了:
$ python3 stack44661467.py
['ø' 'æ' 'å']
some text with ø, æ, å
即 h5py
将字符串 see/interpret 作为 unicode - 写入和读取。
使用转储实用程序:
$ h5dump deleteme.hdf5
HDF5 "deleteme.hdf5" {
GROUP "/" {
DATASET "text" {
DATATYPE H5T_STRING {
STRSIZE H5T_VARIABLE;
STRPAD H5T_STR_NULLTERM;
CSET H5T_CSET_UTF8;
CTYPE H5T_C_S1;
}
DATASPACE SIMPLE { ( 3 ) / ( 3 ) }
DATA {
(0): "777777703777777670", "777777703777777646",
(2): "777777703777777645"
}
ATTRIBUTE "1" {
DATATYPE H5T_STRING {
STRSIZE H5T_VARIABLE;
STRPAD H5T_STR_NULLTERM;
CSET H5T_CSET_UTF8;
CTYPE H5T_C_S1;
}
DATASPACE SCALAR
DATA {
(0): "some text with 777777703777777670, 777777703777777646, 777777703777777645"
}
}
}
}
}
请注意,在这两种情况下,datatype
都标记为 UTF8
DATATYPE H5T_STRING {
STRSIZE H5T_VARIABLE;
STRPAD H5T_STR_NULLTERM;
CSET H5T_CSET_UTF8;
CTYPE H5T_C_S1;
}
文档是这么说的:
http://docs.h5py.org/en/latest/strings.html#variable-length-utf-8
They can store any character a Python unicode string can store, with the exception of NULLs. In the file these are created as variable-length strings with character set H5T_CSET_UTF8.
让 h5py
(或其他 reader)担心将 777777703777777670
解释为正确的 unicode 字符。