强制复合数据类型中的字符串类型

时间:2016-07-13 10:05:02

标签: python numpy h5py

我有一个hdf5文件,其中包含一个具有以下格式属性的组(h5dump):

ATTRIBUTE "PrioInfo" {
    DATATYPE  H5T_COMPOUND {
        H5T_STRING {
            STRSIZE 128;
            STRPAD H5T_STR_NULLTERM;
            CSET H5T_CSET_ASCII;
            CTYPE H5T_C_S1;
    } "FuncName";
        H5T_STD_I32LE "FuncPrio";
    }

现在我读了这个属性,修改它并关闭hdf5文件

>>> f =h5py.File("ooa_pop-Sapiens_ooa__500000.qdf", "r+")
>>> ooa = f['Populations']['Sapiens_ooa']
>>> ooa.attrs['PrioInfo']
array([('ConfinedMove', 8), ('Fertility', 2), ('GetOld', 8),
       ('MultiEvaluator', 5), ('NPPCapacity', 1), ('RandomPair', 3),
       ('Verhulst', 6), ('WeightedMove', 7)], 
      dtype=[('FuncName', 'S128'), ('FuncPrio', '<i4')])
>>> prios = ooa.attrs['PrioInfo']  
>>> prios[6] = ('Verhulst2', 8)
>>> del ooa.attrs['PrioInfo']  
>>> ooa.attrs['PrioInfo'] = prios
>>> f.flush()
>>> f.close()

在修改后的hdf5文件的h5dump中,attibute的类型已经改变:

 ATTRIBUTE "PrioInfo" {
    DATATYPE  H5T_COMPOUND {
       H5T_STRING {
          STRSIZE 128;
          STRPAD H5T_STR_NULLPAD;
          CSET H5T_CSET_ASCII;
          CTYPE H5T_C_S1;
       } "FuncName";
       H5T_STD_I32LE "FuncPrio";
    }

所有字符串都由h5dump显示,填充超过一百个零:

"ConfinedMove\000\000\000\000\000\000\000\000...\000\000\000"

如何强制字符串的类型为“STRPAD H5T_STR_NULLTERM”而不是“STRPAD H5T_STR_NULLPAD”?

0 个答案:

没有答案