无法读取 netcdf 文件属性,因为属性名称包含括号

Failed to read netcdf file attribute, since the attribute name contains parentheses

我正在使用 python-2.7 和 NetCDF4 模块读取 netcdf 文件。我需要从名称中包含括号的 netcdf 文件中读取一个全局属性 (Lat(Degrees.Cents_of_degrees)),但我无法读取它。我做什么:

from netCDF4 import Dataset as NetCDFFile
filename = 'DDE30.2002.RG300004.nc'
nc = NetCDFFile(filename)
lat = nc.Lat(Degrees.Cents_of_degrees)

但我明白了:

AttributeError: NetCDF: Attribute not found

我试过在括号前使用反斜杠,但没有用。 Here 是 netcdf 文件。

这是一个应该适用于您的情况的技巧,但我不建议一概而论。当您使用 nc.myattribute 访问 netCDF 文件的属性时,您实际上是在调用 nc.__getattribute__('myattribute')(请参阅 documentation)。如您所见,输入属性在这里是一个字符串,因此在允许值方面更加宽松。方法名称中的下划线表示该库的作者不想公开此功能。

因此,您可以做的是:

lat = nc.__getattribute__('Lat(Degrees.Cents_of_degrees)') 

这会将 44.120147251339418 分配给 lat

我想你想要 getncattr [docs]。可能最好不要依赖 @M.T.

建议的私有方法
lat = nc.getncattr('Lat(Degrees.Cents_of_degrees)')