Change string representation as number
This commit is contained in:
34
summarize.py
34
summarize.py
@@ -26,15 +26,45 @@ straftat_zuordnung = {
|
||||
"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)))
|
||||
for file in glob.glob("*.csv"):
|
||||
with open(file, encoding='utf-8') as csvfile:
|
||||
csvreader = csv.reader(csvfile, delimiter=';')
|
||||
next(csvreader, None)
|
||||
delikt_code = file.rpartition('.')[0]
|
||||
print(file)
|
||||
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]]["2024"] = {"erffä": row[5], "aufgefä": row[6], "aufgerel": row[7]}
|
||||
complete_data[row[0]][row[1]][straftat_zuordnung[delikt_code]]["2023"] = {
|
||||
"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:
|
||||
json.dump(complete_data, fp, ensure_ascii=False, indent=4)
|
||||
|
||||
Reference in New Issue
Block a user