diff --git a/addr_to_coord.py b/addr_to_coord.py index a183bac..1b4066c 100644 --- a/addr_to_coord.py +++ b/addr_to_coord.py @@ -8,15 +8,22 @@ conn = httplib.HTTPSConnection("geocode-maps.yandex.ru") output_file = open('output.csv', 'w') +errors_file = open('errors.log', 'w') -with open('input2.csv', 'r') as input_file: +def errorMessage(err_file, addr, line, message): + msg = 'line ' + str(line) + ' <' + addr + '> - ' + message + print msg + err_file.write(msg + '\n') + +with open('input3_short.csv', 'r') as input_file: next(input_file) # skip first line line_number = 2 for input_line in input_file: cells = input_line.split(';') if len(cells) > 1: - if len(cells[0]) > 0 and len(cells[1]) > 0: - address = cells[0] + ' ' + cells[1] + action_point = cells[0].translate(None, ' \n\t\r') + address = cells[1].translate(None, ' \n\t\r') + if len(address) > 0: address_encoded = urllib.quote(address) conn.request("GET", "/1.x/?format=json&results=1&geocode=" + address_encoded) @@ -26,19 +33,19 @@ json_resp = json.loads(resp.read()) try: coord_str = json_resp[u'response'][u'GeoObjectCollection'][u'featureMember'][0][u'GeoObject'][u'Point'][u'pos'] - output_file.write(coord_str + '\n') - print coord_str + output_file.write(coord_str + ' ') + output_file.write(action_point + '\n') + print coord_str, action_point except: - print 'Something wrong with format of geocoder response for line', line_number + errorMessage(errors_file, address, line_number, 'something wrong with format of geocoder response') else: - print 'Error occured during request for address', address, 'at line', line_number - + errorMessage(errors_file, address, line_number, 'error occured during HTTP request') else: - print 'Street name or building number is empty at line', line_number + errorMessage(errors_file, '?', line_number, 'address is empty') else: - print 'Not enough data in line', line_number - #time.sleep(0.05) + errorMessage(errors_file, '?', line_number, 'not enough data') line_number += 1 output_file.close() +errors_file.close()