【问题标题】:AttributeError: 'NoneType' object has no attribute 'longitude'AttributeError:“NoneType”对象没有属性“经度”
【发布时间】:2021-12-20 05:47:44
【问题描述】:

我正在尝试获取数据框的“城市”列的坐标。该城市列的 dtype 为 ('O')。它是一个城市名称列表。我已确保城市名称中没有数字或数字。这是我运行来获取坐标的函数:

def get_coordinates(city_list):
    """Takes a list of cities and returns a dictionary of the cities and their corresponding coordinates."""
    geolocator = Nominatim(user_agent='location script')
    dicto = {}


    for city in city_list:
        


        try:
            location = geolocator.geocode(city)
        except:
            raise Exception("There was a problem with the getCoordinates function")
        coordinate_values = (location.longitude, location.latitude)  #in geopandas, the x value corresponds to the longitude while the y value, the latitude(Just in case you were wondering why it was *location.longitude, location.latitude* and not the other way round )
        dicto[city] = coordinate_values #adding the coordinate pair to the dictionary at the end of every loop
    return dicto #finally retruns the dict 

然后:

#getting coordinates for each city in the list
city_coords_dict = get_coordinates(city_list)
city_coords_dict

最终输出:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-63-dedf916e4307> in <module>
      1 #getting coordinates for each city in the list
----> 2 city_coords_dict = get_coordinates(city_list)
      3 city_coords_dict

<ipython-input-62-c391f86b76db> in get_coordinates(city_list)
     13         except:
     14             raise Exception("There was a problem with the getCoordinates function")
---> 15         coordinate_values = (location.longitude, location.latitude)  #in geopandas, the x value corresponds to the longitude while the y value, the latitude(Just in case you were wondering why it was *location.longitude, location.latitude* and not the other way round )
     16         dicto[city] = coordinate_values #adding the coordinate pair to the dictionary at the end of every loop
     17     return dicto #finally retruns the dict

AttributeError: 'NoneType' object has no attribute 'longitude'

任何有建议的人将不胜感激。提前致谢。

【问题讨论】:

  • 您的错误意味着geolocator.geocode(city) 正在为您的一个城市返回None。

标签: python pandas jupyter-notebook coordinates geopy


【解决方案1】:

您的地理定位器显然没有它试图请求的特定城市的数据。作为not_speshal also said,geolocator.geocode(city) 在某个时刻返回None,它被分配给location。所以稍后,您尝试获取location.longitude,因为location 现在是None,所以相当于(None).longitude...

【讨论】:

  • 我能理解这一切,这就是为什么我要求建议或替代方案
  • 请在您的问题中添加更多信息,例如失败的值。
  • 我添加了:` for city in city_list: if 'None' not in city_list:` 当我使用这个 'IF' 语句运行它时,实际上输出给了我第一个城市坐标一本字典。然而,只有一个,第一个。是什么阻止了剩下的事情?
【解决方案2】:

geocode 不会抛出异常,而是返回一个 NoneType 对象。因此,Nominatim 无法找到“位置脚本”中列出的位置之一。

尝试用assert location, city替换try catch块

【讨论】:

  • 当我尝试这样做时,输出:异常:getCoordinates 函数存在问题
  • 嗨,我认为 try catch 块仍然存在。它准确地打印了 catch 语句的输出。如果不清楚,我的意思是删除包含 4 行的行,并在 try: ... 之后删除两行:1:location = geolocator.geocode(city) 和 2:assert location,city。
猜你喜欢
  • 2019-01-01
  • 2021-12-26
  • 2019-07-23
  • 2018-05-13
  • 2020-09-07
  • 2017-05-03
  • 2023-03-16
  • 2018-07-14
  • 2013-06-16
相关资源
最近更新 更多