简体   繁体   中英

Saving Python dictionary to netCDF4 file

I am learning to use Python module for netCDF4 (and netCDF4 in general). Using HDF5 file format, there is a natural way to translate Python dictionaries to HDF5 data structure - using group's attributes:

parameters = {'a':1.0, 'b':2.0, 'c':3.0}
f = h5py.File("test.hdf5",'w')
hdf_parms = f.create_group("parameters")
for k,v in parameters.items():
        hdf_parms.attrs[k]=v

What is the smart netCDF4 way to do this?

Something like:

parameters = {'a':1.0, 'b':2.0, 'c':3.0}
f = netCDF4.Dataset('test.nc', 'w')
parms = f.createGroup('parameters')
for k,v in parameters.items():
    setattr(parms, k, v)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM