File: json_att.py

package info (click to toggle)
netcdf4-python 1.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,588 kB
  • sloc: python: 6,002; ansic: 854; makefile: 15; sh: 2
file content (24 lines) | stat: -rw-r--r-- 688 bytes parent folder | download | duplicates (2)
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()