import os
from os import path

import geocoder
import ruamel.yaml
import xmltodict

data_folder = path.join(os.path.dirname(__file__), "../data")

with open(path.join(data_folder, "events.xml")) as events_file:
    events_file_content = events_file.read()
    events = xmltodict.parse(events_file_content)['events']['event']

with open(path.join(data_folder, "cities.yml")) as cities_file:
    cities_array = ruamel.yaml.load(cities_file, ruamel.yaml.RoundTripLoader)

cities = {}
for city in cities_array:
    cities[city['name']] = city['geo']

for event in events:
    if 'location' not in event:
        print "Location of '" + event['title'] + "' is not specified"
        continue
    location = event['location']
    if location in cities:
        continue
    geocoded_coordinates = geocoder.google(location).latlng
    for city in cities_array:
        coordinates = city['geo']
        if abs(coordinates['lat'] - geocoded_coordinates[0]) < 0.1 and abs(coordinates['lng'] - geocoded_coordinates[1]) < 0.1:
            is_same = None
            while is_same is None:
                user_input = raw_input("Are " + location + " and " + city['name'] + " the same place? (y/n)")
                if user_input == "y" or user_input == "yes":
                    is_same = True
                elif user_input == "n" or user_input == "no":
                    is_same = False
                else:
                    print "Please, enter 'y' or 'n'"
            if is_same:
                location_template = "<location>{0}</location>"
                old_location = location_template.format(event['location'])
                new_location = location_template.format(city['name'])
                events_file_content = events_file_content.replace(old_location, new_location)
                break
    else:
        print location + " geocoded location " + \
              "https://www.google.com/maps/@" + str(geocoded_coordinates[0]) + "," + str(geocoded_coordinates[1]) + ",15z"
        city = {
            'name': location,
            'geo': {
                'lat': geocoded_coordinates[0],
                'lng': geocoded_coordinates[1],
            }
        }
        cities[city['name']] = city
        cities_array = [city] + cities_array

with open(path.join(data_folder, "cities.yml"), 'w') as cities_file:
    ruamel.yaml.dump(cities_array, stream=cities_file, Dumper=ruamel.yaml.RoundTripDumper, allow_unicode=True)

with open(path.join(data_folder, "events.xml"), 'w') as events_file:
    events_file.write(events_file_content)

@@ -1,12 +1,26 @@
+import codecs%0A
 import os%0Afr
@@ -17,16 +17,16 @@
 port os%0A
-
 from os 
@@ -151,32 +151,39 @@
 ../data%22)%0A%0Awith 
+codecs.
 open(path.join(d
@@ -203,24 +203,42 @@
 events.xml%22)
+, encoding='utf-8'
 ) as events_
@@ -1549,16 +1549,17 @@
 plate = 
+u
 %22%3Clocati
@@ -1816,16 +1816,185 @@
 cation)%0A
+                with codecs.open(path.join(data_folder, %22events.xml%22), 'w', encoding='utf-8') as events_file:%0A                    events_file.write(events_file_content)%0A
         
@@ -2460,17 +2460,24 @@
 s_array%0A
-%0A
+        
 with ope
@@ -2533,24 +2533,32 @@
 ities_file:%0A
+        
     ruamel.y
@@ -2660,117 +2660,4 @@
 ue)%0A
-%0Awith open(path.join(data_folder, %22events.xml%22), 'w') as events_file:%0A    events_file.write(events_file_content)%0A

Write to file on each iteration.