Skip to content

Instantly share code, notes, and snippets.

@omad
Created February 18, 2016 00:11
Show Gist options
  • Save omad/936cea57873a29c34de8 to your computer and use it in GitHub Desktop.
Save omad/936cea57873a29c34de8 to your computer and use it in GitHub Desktop.
NetCDF4 Python String Attribute Experiments
import netCDF4
import sys
filename = sys.argv[1]
unicode_string = u"\N{GREEK CAPITAL LETTER DELTA}"
normal_string = "bob"
nc = netCDF4.Dataset(filename, 'w')
nc.uni = unicode_string
nc.encoded_uni = unicode_string.encode('utf8')
nc.normal_string = normal_string
nc.encoded_normal_string = normal_string.encode('utf8')
nc.close()
$ python2 netcdfstringstest.py py2test.nc
$ python3 netcdfstringstest.py py3test.nc
$ ncdump py2test.nc
netcdf py2test {
// global attributes:
string :uni = "Δ" ;
:encoded_uni = "Δ" ;
:normal_string = "bob" ;
:encoded_normal_string = "bob" ;
}
$ ncdump py3test.nc
netcdf py3test {
// global attributes:
string :uni = "Δ" ;
:encoded_uni = "Δ" ;
string :normal_string = "bob" ;
:encoded_normal_string = "bob" ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment