Поймать ошибку в функции
from geopy.extra.rate_limiter import RateLimiter
df2 = pd.read_excel('NEW_ОЭК 2019-2020.xlsx')
geolocator = Nominatim(user_agent='DESKTOP-IPUFB3P')
geocode = RateLimiter(geolocator.geocode, min_delay_seconds=0.5)
#geocode = partial(geolocator.geocode, language="ru")
for index,string in enumerate(df2['Address'][480:]):
print(string)
print(index)
if (string == np.nan) or (string is None):
index+=1
print("Fack")
continue
else:
try:
location = geocode(string)
print(location)
print((location.latitude, location.longitude))
except:
print("Что-то пошло не так")
который прекрасно ловит ошибку, НО когда я хочу тоже самое написать в функции:
from functools import partial
from geopy.geocoders import Nominatim
from geopy.extra.rate_limiter import RateLimiter
geolocator = Nominatim(user_agent='DESKTOP-IPUFB3P')
geocode = RateLimiter(geolocator.geocode, min_delay_seconds=0.5)
#geocode = partial(geolocator.geocode, language="ru")
# def geopy (address):
# if address == None:
# return 'Неточный адрес'
# else:
# location = geocode(string)
# print(location)
# print((location.latitude, location.longitude))
# return location.latitude, location.longitude
def geopy (address):
try:
location = geocode(string)
print(location)
print((location.latitude, location.longitude))
return location.latitude, location.longitude
except:
return 'error:' + str(address)
# location = geocode(address)
# print(address)
# print(location.latitude, location.longitude)
# return location.latitude, location.longitude
def mistake(address):
if address == None:
return 'Неточный адрес'
else:
return 'Error'
df = pd.read_excel('NEW_ОЭК 2019-2020.xlsx')
df['Lat and Long'] = df['Address'].apply(geopy)
df.to_excel('NEW_ОЭК4 2019-2020.xlsx')
df.head()
То ничего не работает! Что делать?
