导航

Python3 HDF5 中英文混合存储

Posted on 2020-09-10 12:28  DotNet1010  阅读(277)  评论(0编辑  收藏  举报
import numpy as np
import sys
import h5py as h5
import os
print("开始")
print("系统默认编码:{}".format(sys.stdout.encoding))

def test_002():
    dt_str = h5.special_dtype(vlen=str)
    student = np.dtype([('name',dt_str), ('age', 'i1'), ('marks', 'f4')]) 
    cn_test = np.array([('abc中国123', 21, 50),('xyz', 18, 75)], dtype = student)
    print(cn_test)
    file_name="deleteme.hdf5"
    if(os.path.isfile(file_name)):
        os.remove(file_name)
    my_hdf5=h5.File(file_name,mode="a")
    my_hdf5["gp03"]=cn_test
    my_hdf5.flush()
    my_hdf5.close()

    my_hdf5 = h5.File(file_name,'r')
    print("read cn data")
    print(my_hdf5['gp03'][:])
    my_hdf5.close()


test_002()