python3.x中的netcdf对某些函数不起作用

python3.x中的netcdf对某些函数不起作用,python,python-3.x,netcdf,Python,Python 3.x,Netcdf,我注意到python3的netcdf函数variables.keys()没有按预期工作。与使用python2时返回键列表(字典)不同,它返回整个信息块,就像调用不带括号的函数时一样。 这并不是python3中唯一一个以这种方式运行的函数。我的理解是netcdf4库已经移植到py3 以下是该函数的预期输出: In [5]: nc.variables.keys() Out[5]: [u'area', u'lat', u'lat_bnds', u'lon', u'lon_bnds', u'

我注意到python3的netcdf函数variables.keys()没有按预期工作。与使用python2时返回键列表(字典)不同,它返回整个信息块,就像调用不带括号的函数时一样。 这并不是python3中唯一一个以这种方式运行的函数。我的理解是netcdf4库已经移植到py3

以下是该函数的预期输出:

In [5]: nc.variables.keys()
Out[5]: 
[u'area',
 u'lat',
 u'lat_bnds',
 u'lon',
 u'lon_bnds',
 u'msk_rgn',
 u'plev',
 u'pr',
 u'tas',
 u'time',
 u'time_bnds',
 u'ua']
而这正是所产生的结果:

In [15]: nc.variables.keys()
Out[15]: 
KeysView(OrderedDict([('area', <class 'netCDF4.Variable'>
float32 area(lat, lon)
    long_name: Surface area
    units: meter2
unlimited dimensions: 
current shape = (128, 256)
filling off
), ('lat', <class 'netCDF4.Variable'>
float32 lat(lat)
    long_name: latitude
    units: degrees_north
    axis: Y
    standard_name: latitude
    bounds: lat_bnds
unlimited dimensions: 
current shape = (128,)
filling off
), ('lat_bnds', <class 'netCDF4.Variable'>
float64 lat_bnds(lat, bnds)
unlimited dimensions: 
current shape = (128, 2)
filling off
), ('lon', <class 'netCDF4.Variable'>
float32 lon(lon)
    long_name: longitude
    units: degrees_east
    axis: X
    standard_name: longitude
    bounds: lon_bnds
unlimited dimensions: 
current shape = (256,)
filling off
), ('lon_bnds', <class 'netCDF4.Variable'>
float64 lon_bnds(lon, bnds)
unlimited dimensions: 
current shape = (256, 2)
filling off
), ('msk_rgn', <class 'netCDF4.Variable'>
int32 msk_rgn(lat, lon)
    long_name: Mask region
    units: bool
unlimited dimensions: 
current shape = (128, 256)
filling off
), ('plev', <class 'netCDF4.Variable'>
float64 plev(plev)
    long_name: pressure
    units: Pa
    standard_name: air_pressure
    positive: down
    axis: Z
unlimited dimensions: 
current shape = (17,)
filling off
), ('pr', <class 'netCDF4.Variable'>
float32 pr(time, lat, lon)
    comment: Created using NCL code CCSM_atmm_2cf.ncl on
 machine eagle163s
    missing_value: 1e+20
    _FillValue: 1e+20
    cell_methods: time: mean (interval: 1 month)
    history: (PRECC+PRECL)*r[h2o]
    original_units: m-1 s-1
    original_name: PRECC, PRECL
    standard_name: precipitation_flux
    units: kg m-2 s-1
    long_name: precipitation_flux
    cell_method: time: mean
unlimited dimensions: time
current shape = (1, 128, 256)
filling off
), ('tas', <class 'netCDF4.Variable'>
float32 tas(time, lat, lon)
    comment: Created using NCL code CCSM_atmm_2cf.ncl on
 machine eagle163s
    missing_value: 1e+20
    _FillValue: 1e+20
    cell_methods: time: mean (interval: 1 month)
    history: Added height coordinate
    coordinates: height
    original_units: K
    original_name: TREFHT
    standard_name: air_temperature
    units: K
    long_name: air_temperature
    cell_method: time: mean
unlimited dimensions: time
current shape = (1, 128, 256)
filling off
), ('time', <class 'netCDF4.Variable'>
float64 time(time)
    calendar: noleap
    standard_name: time
    axis: T
    units: days since 0000-1-1
    bounds: time_bnds
    long_name: time
unlimited dimensions: time
current shape = (1,)
filling off
), ('time_bnds', <class 'netCDF4.Variable'>
float64 time_bnds(time, bnds)
unlimited dimensions: time
current shape = (1, 2)
filling off
), ('ua', <class 'netCDF4.Variable'>
float32 ua(time, plev, lat, lon)
    comment: Created using NCL code CCSM_atmm_2cf.ncl on
 machine eagle163s
    missing_value: 1e+20
    cell_methods: time: mean (interval: 1 month)
    long_name: eastward_wind
    history: Interpolated U with NCL 'vinth2p_ecmwf'
    units: m s-1
    original_units: m s-1
    original_name: U
    standard_name: eastward_wind
    _FillValue: 1e+20
unlimited dimensions: time
current shape = (1, 17, 128, 256)
filling off
)]))
[15]中的
:nc.variables.keys()
出[15]:
键视图(OrderedDict([('area'),
浮动区域(横向、纵向)
长名称:表面积
单位:米2
无限尺寸:
当前形状=(128256)
填补
),('lat',
浮动32 lat(lat)
长名称:纬度
单位:北纬度
轴:Y
标准名称:纬度
边界:lat_bnds
无限尺寸:
当前形状=(128,)
填补
),(“lat_bnds”,
浮动64个纬度(纬度,纬度)
无限尺寸:
当前形状=(128,2)
填补
),(“lon”,
浮动32 lon(lon)
长名称:经度
单位:东纬度
轴:X
标准名称:经度
边界:lon_bnds
无限尺寸:
当前形状=(256,)
填补
),(“lon_bnds”,
浮点数64 lon_bnds(lon,bnds)
无限尺寸:
当前形状=(256,2)
填补
),(“msk_rgn”,
int32 msk_rgn(纬度、经度)
长名称:遮罩区域
单位:布尔
无限尺寸:
当前形状=(128256)
填补
),(“plev”,
浮动64磅/平方英寸(磅/平方英寸)
长名称:压力
单位:Pa
标准名称:气压
正面:向下
轴:Z
无限尺寸:
当前形状=(17,)
填补
),(“pr”,
浮动32 pr(时间、纬度、经度)
注释:在上使用NCL代码CCSM_atmm_2cf.NCL创建
机器鹰163S
缺少_值:1e+20
_填充值:1e+20
单元法:时间:平均值(间隔:1个月)
历史:(PRECC+PRECL)*r[h2o]
原始单位:m-1 s-1
原名:PRECC,PRECL
标准名称:沉淀通量
单位:kg m-2 s-1
长\u名称:降水量\u流量
单元法:时间:平均值
无限维度:时间
当前形状=(1128256)
填补
),(“tas”,
浮动32 TA(时间、纬度、经度)
注释:在上使用NCL代码CCSM_atmm_2cf.NCL创建
机器鹰163S
缺少_值:1e+20
_填充值:1e+20
单元法:时间:平均值(间隔:1个月)
历史记录:添加高度坐标
坐标:高度
原始单位:K
原名:TREFHT
标准名称:空气温度
单位:K
长名称:空气温度
单元法:时间:平均值
无限维度:时间
当前形状=(1128256)
填补
),(“时间”,
浮动64次(次)
日历:noleap
标准名称:时间
轴:T
单位:自0000-1-1起的天数
界限:时间
名字:时间
无限维度:时间
当前形状=(1,)
填补
),(“时间”,
浮动64时间(时间,bnds)
无限维度:时间
当前形状=(1,2)
填补
),(“ua”,
浮动32 ua(时间、plev、lat、lon)
注释:在上使用NCL代码CCSM_atmm_2cf.NCL创建
机器鹰163S
缺少_值:1e+20
单元法:时间:平均值(间隔:1个月)
龙名:东风
历史:使用NCL'vinth2p_ecmwf'插值U
单位:m s-1
原始单位:m s-1
原名:U
标准名称:东风
_填充值:1e+20
无限维度:时间
当前形状=(1,17,128,256)
填补
)]))

这不是一个bug,而是字典更改的结果,特别是在Python 3.x中的
orderedict
。在Python3.x中,
keys()
方法不再返回键列表,而是返回迭代器对象。您看到的输出只是迭代器对象(
KeysView
)及其对原始字典的引用,然后显示其表示形式

若您需要一个键列表,您需要特别询问:
list(nc.variables.keys())