如何忽略AttributeError:'NoneType'

Don*_*e Z 1 python attributeerror geopy nonetype

location = geolocator.geocode(" ".join(address.values()))
if location.longitude is not None:
    node['pos'] = [location.longitude, location.latitude]
Run Code Online (Sandbox Code Playgroud)

不明白的方式我仍然得到这个错误:

  File "/home/easypc/Documents/Udacity_nano_degree/Data_Wrangling/audit_vilnius.py", line 167, in shape_element
    if location.longitude is not None:
AttributeError: 'NoneType' object has no attribute 'longitude'
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 7

它是location变量本身就是None,测试它:

if location is not None and location.longitude is not None:
Run Code Online (Sandbox Code Playgroud)

或者更简单地说:

if location:
Run Code Online (Sandbox Code Playgroud)