Created
February 11, 2015 10:49
-
-
Save osya/aef0bbd580b88720ff90 to your computer and use it in GitHub Desktop.
Example of using csv & geopy #csv #geopy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__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