주피터 주소 위경도 변환 코드 에러 AttributeError: 'NoneType' object has no attribute 'latitude'
조회수 1394회
https://wonhwa.tistory.com/29?category=996516 이 게시물을 토대로 생애 난생 첫 코드라는 것을 만져봤습니다. 친구의 도움으로 환경설정도 하였습니다
import pandas as pd
csv = pd.read_csv('../data/lon.csv',encoding='cp949')
csv
pip install geopy
from geopy.geocoders import Nominatim
geo_local = Nominatim(user_agent='South Korea')
address= csv['주소']
print(address.head())
def geocoding(address):
geo = geo_local.geocode(address)
x_y = [geo.latitude, geo.longitude]
return x_y
여기까지는 아무 문제 없이 잘 진행 되었으나 문제는 다음입니다. 앞에 말한 싸이트와 똑같이 실행하였으나
latitude = []
longitude = []
for i in address:
latitude.append(geocoding(i)[0])
longitude.append(geocoding(i)[1])
--
AttributeError Traceback (most recent call last)
<ipython-input-26-ede2a66771c9> in <module>
4
5 for i in address:
----> 6 latitude.append(geocoding(i)[0])
7 longitude.append(geocoding(i)[1])
<ipython-input-25-2c4629605003> in geocoding(address)
1 def geocoding(address):
2 geo = geo_local.geocode(address)
----> 3 x_y = [geo.latitude, geo.longitude]
4 return x_y
AttributeError: 'NoneType' object has no attribute 'latitude'
이런 에러가 뜹니다. 앞서 말씀드렸다시이 난생 첫 코드를 만져보니 최대한 자세하고 쉽게 알려 주시면 정말 감사하겠습니다.
1 답변
-
2 geo = geo_local.geocode(address) ----> 3 x_y = [geo.latitude, geo.longitude] 4 return x_y AttributeError: 'NoneType' object has no attribute 'latitude'
에러메시지를 해석해 보면, 'NoneType' 객체는 'latitude' 애트리뷰트가 없습니다. 라는 뜻입니다.
이 에러가 가르키는 코드는
geo.latitude
라는 코드입니다.geo
가None Type
이고, 이 타입의 객체인geo
에는latitude
란 애트리뷰트가 없다는 말입니다.NoneType
객체는None
뿐입니다.그럼 왜
geo
가None
인가 하면, 앞쪽에geo = geo_local.geocode(address)
에서geocode
함수가None
을 반환했기 때문일 겁니다.주소에 대해 위도와 경도를 반환해주는 함수일텐데, 주소문자열에 주소가 아닌 문자열이 들어가 있거나, geocode 함수가 주어진 주소를 알지 못한다면, 위도경도를 알지 못하기 때문에, None을 반환하는 것 같네요.
댓글 입력