错误消息" Nontype对象没有get属性"

时间:2017-09-14 16:39:51

标签: python json dictionary

我使用API​​检索了一些数据并尝试使用此python脚本提取国家/地区代码但收到错误消息:

  

Nonetype对象没有get属性。

数据位于JSON文件中。这是我的代码:

country_code=[data.get("sys").get("country") for data in weather_data]. 

1 个答案:

答案 0 :(得分:1)

如果来自不包含'key-x'的字典的.get('key-x', default_return_value),将返回。您可以通过指定default_return_value来更改此行为,其中.get是一个变量,如果该密钥不存在,您希望从调用返回该值。在这种情况下,您链接了两个.get调用,其中第一个.get调用似乎重新调整为None,此类型与字典不同,没有data函数。我将通过在此处打印出'sys'变量的内容并查看其中是否存在p span {display:inline-block;} 密钥来开始调试。