Attributeerror: 'Nonetype' Object has no attribute'latitude' _ Python

Asked

Viewed 220 times

0

I’ve seen some similar questions as mine here, but since I’m very new to Python, I’m not getting around to making my mistake. My program is very short, I simply want to find the coordinates of latitude and longitude from names of municipalities that I read from a CSV (Listaend.csv). When running the program I get the error message:

Traceback (Most recent call last): File "teste2.py", line 26, in print(Location.address) Attributeerror: 'Nonetype' Object has no attribute 'address'

The program is:

import geopandas as gpds
import pandas as pds 
from geopy.geocoders import Nominatim

df = pds.read_csv("ListaEnd.csv", encoding='UTF8')

geolocator = Nominatim(user_agent="Intro Geocode")
location = geolocator.geocode(df['Municipio'])

print(location.address)
print((location.latitude, location.longitude))

Any help is very valid, friends. Since now, it has been worth too much!!!

  • What is the value of df['Municipio'], you have a string with the address or maybe a list?

  • df['Municipios'] are names of Brazilian municipalities, such as: Ji-Paraná, Campo Grande, Iporá, ...

1 answer

1

NoteType means that instead of an instance of any class you are working with, you received the value None. This usually means that the function called failed or returned an unexpected result.

In that case, maybe you can handle the mistake in a better way, like:

try:
    location = geolocator.geocode(df['Municipio'], timeout=10)
    print(location.address)
except AttributeError:
    print("Problema com os dados ou uma falha com o Geocode."
except GeocoderTimedOut:
    print("Um erro de timeout ocorreu.")
  • Jobert, if I understand correctly, the program was like this after the modification you advised me:

  • Jobert, thanks for the suggestion, but it hasn’t worked yet.... I got the answer: File "teste3.py", line 10
 print(location.address)
 ^
IndentationError: unexpected indent

  • @RAULGBMENDES This usually means that there are some extra spaces in the indentation on the indicated line. Checks if this line has the same amount of spaces as in the previous lines with respect to the code block of the try.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.