Skip to content

Instantly share code, notes, and snippets.

@osya
Created February 11, 2015 10:49
Show Gist options
  • Save osya/aef0bbd580b88720ff90 to your computer and use it in GitHub Desktop.
Save osya/aef0bbd580b88720ff90 to your computer and use it in GitHub Desktop.
Example of using csv & geopy #csv #geopy
__author__ = 'vosipov'
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import csv
import codecs
from geopy import Yandex
geolocator = Yandex()
with codecs.open('realty.csv', 'r', 'cp1251') as csvfile:
with open('realty_out.csv', 'w') as csvout:
reader = csv.DictReader(csvfile)
fieldnames = reader.fieldnames
writer = csv.DictWriter(csvout, fieldnames=fieldnames)
writer.writeheader()
for i, row in enumerate(reader):
AdministrativeArea = ''
try:
location = geolocator.geocode(row['ADDRESS'])
AdministrativeArea = location.raw['metaDataProperty']['GeocoderMetaData']['AddressDetails']['Country']['AdministrativeArea']['AdministrativeAreaName']
except:
pass
row['REGIONS'] = AdministrativeArea
writer.writerow(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment