Python 使用.values键“AttributeError:“list”对象没有属性“values”接收字典项时出现问题
运行上述代码时,出现以下错误:Python 使用.values键“AttributeError:“list”对象没有属性“values”接收字典项时出现问题,python,list,dictionary,attributeerror,pygrib,Python,List,Dictionary,Attributeerror,Pygrib,运行上述代码时,出现以下错误: gribfile='gfs20191010.0p25' #file containing 384 hours of forecast data for variable Geopotential Height grbs=pygrib.open(gribfile) grb1=grbs.select(name='Geopotential Height')[0:24] day1 = grb1.values .values键在仅选择列表中的第一项时有效,如下所示: A
gribfile='gfs20191010.0p25' #file containing 384 hours of forecast data for variable Geopotential Height
grbs=pygrib.open(gribfile)
grb1=grbs.select(name='Geopotential Height')[0:24]
day1 = grb1.values
.values键在仅选择列表中的第一项时有效,如下所示:
AttributeError: 'list' object has no attribute 'values'
但当我这样做的时候就不行了
grb1 = grbs.select(name='Geopotential Height')[0]
我知道.values键的格式是dict.values,但我不知道如何检索前24个列表项中的数据。
我的目标是将所选可变位势高度的前24个文件作为一个numpy数组,但为了做到这一点,我需要能够使用.values提取每个文件中包含的信息
根据您提供的文档链接,我一直在以下pygrib文档中找到:: 选择返回gribmessage实例的列表 因此,当您这样做时:
grb1 = grbs.select(name='Geopotential Height')[0:24]
您将获取select的结果,这是一个列表,然后对该列表进行切片,从而生成另一个列表。然后将切片列表绑定到grb1。因此,grb1是gribmessage实例的列表
正如Pyhton异常所述,列表没有values属性。然而,一条gribmessage可能确实如此
不确定这是否使您更接近预期结果,但您可以使用列表理解从列表中的gribmessage实例中获取值:
grb1=grbs.select(name='Geopotential Height')[0:24]
在尝试访问grb1的.values之前,也许您应该确保grb1是您所期望的。显然,这是一个列表。如果[0].values有效,则可能是一个dict列表。在这种情况下,这可能有效:day1=grb1[0].values.grb1[0]。values有效!非常感谢。
values = [grb.values for grb in grb1]