Skip to content

Round trip converts text attributes to strings #529

@jjhelmus

Description

@jjhelmus

When netcdf4-python is used to read a "text" attribute from a file and write that attribute to a second file the resulting attribute will be a string where-as the original attribute was "text".

For example:

import netCDF4

# create a reference netCDF file with a text (non-unicode) attribute
ds1 = netCDF4.Dataset('test1.nc', 'w')
ds1.setncattr('foo', 'bar')
ds1.close()

# copy the attribute to a second file, result is a string attribute
ds1 = netCDF4.Dataset('test1.nc', 'r')
ds2 = netCDF4.Dataset('test2.nc', 'w')
ds2.setncattr('foo', ds1.getncattr('foo'))
ds1.close()
ds2.close()

ncdumps of the two files show that the text attribute was changed to a string attribute.

 ncdump test1.nc; ncdump test2.nc
netcdf test1 {

// global attributes:
        :foo = "bar" ;
}
netcdf test2 {

// global attributes:
        string :foo = "bar" ;
}

From what I can tell this is a result of #389 which addressed #388. Unicode Python variables are written as string attributes (using nc_put_att_string) but reading a "text" or string attribute returns unicode. One possible fix would be to have "text" attribute be returned as str (Python 2) or bytes (Python 3).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions