1import netCDF4
2import numpy as np
3
4str_out = netCDF4.stringtochar(np.array(['test'], 'S4'))
5
6nc = netCDF4.Dataset('./test.nc', 'w', format='NETCDF4_CLASSIC')
7nc.createDimension('lat', 1)
8nc.createDimension('lon', 1)
9nc.createDimension('nchar', 4)
10
11lat = nc.createVariable('lat', 'f4', ('lat',))
12lon = nc.createVariable('lon', 'f4', ('lon',))
13place = nc.createVariable('place', 'S1', ('nchar'))
14
15lat[:] = 17.002
16lon[:] = -81.501
17place[:] = str_out
18
19nc.close()
1nc_f0 = Dataset('path_to_nc','r')
2var = nc_f0.groups['group_name'].variables['var_name']