Change string representation as number

This commit is contained in:
2025-12-06 20:53:44 +01:00
parent 571831d3b3
commit dbdbf0a7a1

View File

@@ -26,15 +26,45 @@ straftat_zuordnung = {
"999999": "Straftaten insgesamt nur Bezirke" "999999": "Straftaten insgesamt nur Bezirke"
} }
def cleanup_text(text):
result = text.replace('.', '')
result = result.replace(' ', '')
result = result.replace('-', '')
return result
def cleanup_int(text):
result = cleanup_text(text)
if result == "":
result = "0"
return int(result)
def cleanup_float(text):
result = cleanup_text(text)
result = result.replace('%', '')
result = result.replace(',', '.')
if result == "":
result = "0.0"
return float(result)
complete_data = defaultdict(lambda: defaultdict(lambda: defaultdict(dict))) complete_data = defaultdict(lambda: defaultdict(lambda: defaultdict(dict)))
for file in glob.glob("*.csv"): for file in glob.glob("*.csv"):
with open(file, encoding='utf-8') as csvfile: with open(file, encoding='utf-8') as csvfile:
csvreader = csv.reader(csvfile, delimiter=';') csvreader = csv.reader(csvfile, delimiter=';')
next(csvreader, None) next(csvreader, None)
delikt_code = file.rpartition('.')[0] delikt_code = file.rpartition('.')[0]
print(file)
for row in csvreader: for row in csvreader:
complete_data[row[0]][row[1]][straftat_zuordnung[delikt_code]]["2023"] = {"erffä": row[2], "aufgefä": row[3], "aufgerel": row[4]} complete_data[row[0]][row[1]][straftat_zuordnung[delikt_code]]["2023"] = {
complete_data[row[0]][row[1]][straftat_zuordnung[delikt_code]]["2024"] = {"erf": row[5], "aufgefä": row[6], "aufgerel": row[7]} "Erfasste Fälle": cleanup_int(row[2]),
"Aufgeklärte Fälle": cleanup_int(row[3]),
"Aufklärung relativ": cleanup_float(row[4]),
}
complete_data[row[0]][row[1]][straftat_zuordnung[delikt_code]]["2024"] = {
"Erfasste Fälle": cleanup_int(row[5]),
"Aufgeklärte Fälle": cleanup_int(row[6]),
"Aufklärung relativ": cleanup_float(row[7]),
}
with open('data.json', 'w', encoding="utf-8") as fp: with open('data.json', 'w', encoding="utf-8") as fp:
json.dump(complete_data, fp, ensure_ascii=False, indent=4) json.dump(complete_data, fp, ensure_ascii=False, indent=4)