· 4 years ago · May 19, 2021, 03:54 PM
1# THIS IS AN EXAMPLE OF THE LIST I GET. I WANT TO CHECK WITH AN IF STATEMENT WHAT IS THE VALUE OF 'location_type'
2
3[{'address_components': [{'long_name': '70', 'short_name': '70', 'types': ['street_number']}, {'long_name': 'Via XXIV Maggio', 'short_name': 'Via XXIV Maggio', 'types': ['route']}, {'long_name': 'Brazzano', 'short_name': 'Brazzano', 'types': ['locality', 'political']}, {'long_name': 'Cormons', 'short_name': 'Cormons', 'types': ['administrative_area_level_3', 'political']}, {'long_name': 'Provincia di Gorizia', 'short_name': 'GO', 'types': ['administrative_area_level_2', 'political']}, {'long_name': 'Friuli-Venezia Giulia', 'short_name': 'Friuli-Venezia Giulia', 'types': ['administrative_area_level_1', 'political']}, {'long_name': 'Italy', 'short_name': 'IT', 'types': ['country', 'political']}, {'long_name': '34071', 'short_name': '34071', 'types': ['postal_code']}], 'formatted_address': 'Via XXIV Maggio, 70, 34071 Brazzano GO, Italy', 'geometry': {'location': {'lat': 45.9736687, 'lng': 13.4551941}, 'location_type': 'ROOFTOP', 'viewport': {'northeast': {'lat': 45.9750176802915, 'lng': 13.4565430802915}, 'southwest': {'lat': 45.9723197197085, 'lng': 13.4538451197085}}}, 'partial_match': True, 'place_id': 'ChIJx6dmD0mse0cRq8tgwLV3YOo', 'plus_code': {'compound_code': 'XFF4+F3 Brazzano GO, Italy', 'global_code': '8FQMXFF4+F3'}, 'types': ['street_address']}]
4
5# THIS IS MY CODE:
6
7import googlemaps
8from datetime import datetime
9import time
10
11gmaps = googlemaps.Client(key='MY API KEY')
12
13
14# Geocoding an address
15
16migliori = open("Corrected-Adresses.txt",'a+')
17nontrovati = open("Adresses-not-found.txt","a+")
18
19print("Welcome to my app")
20
21
22file = open('Adresses-to-edit.txt','r+')
23
24x = -1
25
26print("\n")
27
28for line in file:
29 time.sleep(0.5)
30 name = line
31 x += 1
32 print(f"Lines processed: {x}\nCurrently line: {line}")
33 geocode_result = gmaps.geocode({name})
34
35 if geocode_result == []:
36 nontrovati.write(f"{name}")
37
38 elif geocode_result[0]['location_type'] == 'APPROXIMATE':
39 nontrovati.write(f"{name}")
40 print("APPROXIMATE")
41
42 elif geocode_result[0]['location_type'] == 'ROOFTOP':
43 migliori.write(f"{geocode_result[0]['formatted_address']}\n")
44 print("ROOFTOP")
45
46 elif geocode_result[0]['location_type'] == 'RANGE_INTERPOLATED':
47 migliori.write(f"{geocode_result[0]['formatted_address']}\n")
48 print("RANGE_INTERPOLATED")
49
50 elif geocode_result[0]['location_type'] == 'GEOMETRIC_CENTER':
51 migliori.write(f"{geocode_result[0]['formatted_address']}\n")
52 print("GEOMETRIC_CENTER")
53
54file.close()
55migliori.close()
56nontrovati.close()
57
58print("All addresses processed.")
59