import re import json #Öffnen der Datei Personen.txt zum Lesen with open("Personen.txt", "r") as f: persons = [] lines = f.readlines() for i, line in enumerate(lines): #Aufteilen der Zeile in Spalten cols = line.split(",") person = { "Index": i, "Titel": [None], "Vorname": [], "Zweitname": [None], "Nachname": [], "Geburtsdatum": [], "Straße": [], "Hausnummer": [], "PLZ": [], "Wohnort": [] } match = re.search(r"(.*?)\.", cols[0]) if match: name = re.sub(r"([A-Za-zäöüÄÖÜ]+).([A-Za-zäöüÄÖÜ]-+) ([A-Za-zäöüÄÖÜ]-+)(?: ([A-Za-zäöüÄÖÜ]-+))?",r"\1 \2 \3 \4", cols[0]) # Titel (optional), Vorname, Zweitname (optional) und Nachname parts = name.split() person["Titel"]=parts[0] person["Vorname"] = parts[1] if len(parts) == 3: person["Nachname"]=parts[2] if len(parts) == 4: person["Zweitname"] = parts[2] person["Nachname"] = parts[3] else: name = re.sub(r"([A-Za-zäöüÄÖÜ]-+) ([A-Za-zäöüÄÖÜ]-+)(?: ([A-Za-zäöüÄÖÜ]-+))?", r"\1 \2 \3", cols[0]) # Titel (optional), Vorname, Zweitname (optional) und Nachname parts = name.split() person["Vorname"] = parts[0] if len(parts) == 2: person["Nachname"] = parts[1] if len(parts) == 4: person["Zweitname"] = parts[1] person["Nachname"] = parts[2] print("Index: ", person["Index"]) print("Titel: ", person["Titel"]) print("Vorname: ", person["Vorname"]) print("Zweitname: ", person["Zweitname"]) print("NAchname: ", person["Nachname"]) #Adresse address = re.sub(r"([^\d]+)(\d+)", r"\1 \2", cols[1]) #Straße, Hausnummer parts = address.split() person["Straße"] = [parts[0]] person["Hausnummer"] = [parts[1]] print("Straße: ", person["Straße"]) print("Hausnummer: ", person["Hausnummer"]) # Adresse address = re.sub(r"(\d{5})([^\s]+)", r"\1 \2", cols[2]) #PLZ und Wohnort parts = address.split() person["PLZ"] = [parts[0]] person["Wohnort"] = [parts[1]] print("PLZ: ", person["PLZ"]) print("Wohnort: ", person["Wohnort"]) #Geburtstag person["Geburtsdatum"] = re.sub(r"(\d+)-(\d+)-(\d+)", r"\3.\2.\1", cols[3]) print("Geburtstag: ", person["Geburtsdatum"]) persons.append(person) with open("PersonenNeu.json", "w") as json_file: json.dump(persons, json_file, indent=3, ensure_ascii=False)