Skip to content

netCDF4-python should use nc_put_att_string when writing unicode attributes to netCDF4 files #388

Description

@shoyer

Currently, it uses nc_put_att_text, which means that the resulting attributes are always saved as bytes in the underlying HDF5 files.

This works OK as long as you only read netCDF4 files back with netCDF4-python (netCDF4-python will automatically convert bytes attributes on Python 3 into str).

But the resulting files are unlikely to be portable. For example, h5py reads such attributes back as bytes:

Python 3.4.3 |Continuum Analytics, Inc.| (default, Mar  6 2015, 12:07:41)
Type "copyright", "credits" or "license" for more information.

IPython 3.1.0 -- An enhanced Interactive Python.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import netCDF4

In [2]: ds = netCDF4.Dataset('unicode-str-test.nc', 'w')

In [3]: ds.foo = 'bar'

In [4]: ds.close()

In [5]: import h5py

In [6]: f = h5py.File('unicode-str-test.nc', 'r')

In [7]: f.attrs['foo']
Out[7]: b'bar'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions