-
Notifications
You must be signed in to change notification settings - Fork 0
/
[SK] #08 Wahrheitstest für Ortschaften.py
59 lines (45 loc) · 1.84 KB
/
[SK] #08 Wahrheitstest für Ortschaften.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#08 | WAHRHEITSTEST FÜR ORTSCHAFTEN
#Voraussetzung: Excel-Tabelle mit zwei Spalten. A = DB-ID, B = Geoname-ID, --> Code legt die Daten ab Spalte C ab
import pandas as pd
import requests
# Lese die Excel-Datei
data = pd.read_excel(f"D:\schwabenkinder\schwabenkinder_dok_admex_bool.xlsx", sheet_name='Tabelle1')
counter = 0
counter_apikey = int(counter/900)
# Funktion zum Abfragen der Geonames API
def query_geonames_api(geoname_id):
endpoint = 'http://api.geonames.org/hierarchyJSON'
api_keys = ['xeilian', 'code', 'brain', 'world', 'six', 'power', 'cool', 'help', 'jackie', 'nice', 'swift', 'brian',
'heavy', 'value', 'stop', 'happy', 'moin', 'light']
api_key = api_keys[counter_apikey]
# API-Anfrage senden
params = {
"geonameId": geoname_id,
"username": api_key
}
print(geoname_id)
response = requests.get(endpoint, params=params)
print(f'API-Anfrage für {geoname_id} gesendet')
# Ergebnis verarbeiten
if response.status_code == 200:
data = response.json()
for geoname in data.get('geonames', []):
fcl = geoname.get('fcl')
if fcl == 'P':
return 'P'
return 'X'
return 'False'
# Schleife über die Datensätze in der Excel-Datei
for index, row in data.iterrows():
geoname_id = int(row.iloc[0]) # Verwende Spalte A
# Überprüfen, ob alle benötigten Informationen vorhanden sind
if pd.notnull(geoname_id):
result = query_geonames_api(geoname_id)
print(result)
if result is not None:
# Ergebnisse in die Excel-Datei eintragen
data.loc[index, 'Spalte B'] = result
counter += 1
# Ergebnisse in eine neue Excel-Datei schreiben
data.to_excel(f"D:\schwabenkinder\schwabenkinder_dok_admex_bool.xlsx", sheet_name="Tabelle1", index=False)
print('Vorgang abgeschlossen.')