使用 Twitter 提取地理定位的全名和国家代码

Extracting full name and country code of geolocation with Twitter

我正在尝试从 json 文件中提取 "place" 的全名和国家/地区代码,该文件包含从 Twitter API 中提取的数千条推文。我曾多次尝试编写代码,但下面的代码对我来说最有意义。我能够提取其他数据(主题标签、日期、具有类似编码的关注者)。

for i in range(len(data)):    

    if data[i]["place"] != "null":
        get_place_info  = data[i]["place"].get("full_name")
        print(get_place_info)

    else:
        print("No place defined")

这是我得到的错误:


AttributeError Traceback(最后一次调用) 在 3个 4 如果数据[i]["place"] != "null": ----> 5 get_place_info = 数据[i]["place"].get("full_name") 6 打印(get_place_info) 7

AttributeError: 'NoneType' 对象没有属性 'get'

添加我之前评论的答案...

您是否尝试过将 if 语句更改为

if data[i]['place'] is not None: