Python 3.x 为什么会出现这个错误?TypeError:必须是str,而不是NoneType

Python 3.x 为什么会出现这个错误?TypeError:必须是str,而不是NoneType,python-3.x,discord.py,Python 3.x,Discord.py,我正在尝试使用geoip2库来获取ip地址的位置,但是我一直得到一个类型错误 这段代码以前是有效的,不知道发生了什么 @client.command(name="iplocation") async def ip_location(*,ip): try: reader = geoip2.database.Reader('GeoLite2-City.mmdb') response = reader.city(ip) await client

我正在尝试使用geoip2库来获取ip地址的位置,但是我一直得到一个类型错误

这段代码以前是有效的,不知道发生了什么

@client.command(name="iplocation")
async def ip_location(*,ip):
    try:
        reader = geoip2.database.Reader('GeoLite2-City.mmdb')
        response = reader.city(ip)

        await client.say("Country: " + response.country.iso_code)
        await client.say("City: " + response.subdivisions.most_specific.name)
        await client.say("Postal Code: " + response.postal.code)
        await client.say("Latitude: " + str(response.location.latitude))
        await client.say("Longitude: " + str(response.location.longitude))
        reader.close()
     except geoip2.errors.AddressNotFoundError:
         await client.say("The Address: " + ip + " Is not in the database")
     except ValueError:
         await client.say(ip + " Does not appear to be an IPv4 or IPv6 address.")
我想

国家:美国 城市:纽约 邮政编码:10453 纬度:1.2931 经度:103.8558

这只是一个例子

我的错误:

Traceback (most recent call last):
  File "/home/c0deninja/.local/lib/python3.6/site-packages/discord/ext/commands/core.py", line 50, in wrapped
    ret = yield from coro(*args, **kwargs)
  File "discordbot2.py", line 116, in ip_location
    await client.say("City: " + response.subdivisions.most_specific.name)
TypeError: must be str, not NoneType

当没有城市集时,从中提取数据的数据集必须使用None作为类型。在这种情况下,错误消息是一个很好的提示。您必须允许它通过您所说的转换将None用作字符串,或者测试None,并使用空白字符串代替实际的城市名称

此错误可能发生在您正在使用的数据集中没有字符串集的任何情况下

File "discordbot2.py", line 116, in ip_location
    await client.say("City: " + response.subdivisions.most_specific.name)
TypeError: must be str, not NoneType

跟踪的确切错误是什么?可能是国家、邮政编码或城市没有。您需要用str将它们包装起来,或者找出它们没有的原因。@Carcigenicate编辑了帖子以显示错误消息,我尝试用str包装它们,代码可以工作,但是我得到的城市和邮政编码的输出都是无。然后,响应数据无效/不完整。