【问题标题】:TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' in python类型错误:+ 不支持的操作数类型:\'NoneType\' 和 \'str\' in python
【发布时间】:2023-01-24 16:29:05
【问题描述】:

我正在使用 Python 创建带有库的热图

from pymongo import *
from geopy.geocoders import Nominatim
import folium

在这里,我生成了从 Cloud MongoDB Atlas 数据库中提取数据的脚本。

reviews_by_county = list(collection.aggregate([
    {"$group": {"_id": "$County_en", "count": {"$sum": 1}}},
    {"$sort": {"count": -1}}
]))

我还创建了一个函数来创建该热图

def Create_hitmap(reviews_by_county):
    map = folium.Map(location=[35, 25], zoom_start=5)
    geolocator = Nominatim(user_agent="geoapi", timeout=30)

    # Iterate over the reviews_by_county and add a marker to the map for each county:
    for county in reviews_by_county:
        location = geolocator.geocode(county['_id'])
        if location:
            folium.Marker(location=[location.latitude, location.longitude], 
                      popup=county['_id'] + ': ' + str(county['count'])).add_to(map)
        else:
            print(f"{county['_id']} not found")


    # You can then save the map to an HTML file and view it in a web browser.
    map.save("map.html")

最后,我正在调用提供其中列表的函数。

Create_hitmap(reviews_by_county)

我收到错误:

第 47 行,在 Create_hitmap 中 popup=county['_id'] + ': ' + str(county['count'])).add_to(地图) 类型错误:+ 不支持的操作数类型:“NoneType”和“str”

注意:我已经从我的数据中删除了 Null 值,但我仍然收到这样的错误。

【问题讨论】:

    标签: python heatmap nonetype folium


    【解决方案1】:

    如果您从 reviews_by_county 中删除了所有空值,那么空值可能会从 str(county['count'])).add_to(map) 表达式中返回。

    您可以使用格式化字符串而不是连接(希望是什么)字符串,因此哪个值为 null 会更明显:

    popup = "{0}: {1}".format(county['_id'], str(county['count'])).add_to(map))
    

    【讨论】:

      猜你喜欢
      • 2014-06-15
      • 2018-07-07
      • 2023-01-04
      • 1970-01-01
      • 2017-07-11
      • 2019-08-22
      • 2015-09-21
      • 2017-05-27
      相关资源
      最近更新 更多