forked from Unidata/netcdf4-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_att.py
More file actions
24 lines (24 loc) · 688 Bytes
/
json_att.py
File metadata and controls
24 lines (24 loc) · 688 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from netCDF4 import Dataset
import json
# example showing how python objects (lists, dicts, None, True)
# can be serialized as strings, saved as netCDF attributes,
# and then converted back to python objects using json.
ds = Dataset('json.nc', 'w')
ds.pythonatt1 = json.dumps(['foo', {'bar': ['baz', None, 1.0, 2]}])
ds.pythonatt2 = "true" # converted to bool
ds.pythonatt3 = "null" # converted to None
print(ds)
ds.close()
ds = Dataset('json.nc')
def convert_json(s):
try:
a = json.loads(s)
return a
except:
return s
x = convert_json(ds.pythonatt1)
print(type(x))
print(x)
print(convert_json(ds.pythonatt2))
print(convert_json(ds.pythonatt3))
ds.close()