import re import json index = 0 def parse_zeile(zeile): global index index = index + 1 elemente = zeile.split(", ") name = elemente[0] geburtsdatum = elemente[1] adresse = elemente[2] rufnummer = elemente[3] print(name, adresse, geburtsdatum, rufnummer) person = {} person['Index'] = index name_matches = re.search(r'([^ ]+) ([^ ]+) ([^ ]+)(?: ([^ ]+))?', name) person['Titel'] = [name_matches.group(1)] person['Vorname'] = [name_matches.group(2)] if name_matches.group(4) is None: person['Zweitname'] = [None] person['Nachname'] = [name_matches.group(3)] else: person['Zweitname'] = [name_matches.group(3)] person['Nachname'] = [name_matches.group(4)] adress_matches = re.search(r'([^0-9]+) ([0-9]+) ([0-9]{5,10}) (.+)', adresse) person['Straße'] = [adress_matches.group(1)] person['Hausnummer'] = [adress_matches.group(2)] person['PLZ'] = [adress_matches.group(3)] person['Wohnort'] = [adress_matches.group(4)] birth_matches = re.search(r'([0-9]{2,4})[ .-]([0-9]{2})[ .-]([0-9]{2,4})', geburtsdatum) if len(birth_matches.group(1)) == 4: person['Geburtsdatum'] = [birth_matches.group(3) + "." + birth_matches.group(2) + "." + birth_matches.group(1)] else: person['Geburtsdatum'] = [birth_matches.group(1) + "." + birth_matches.group(2) + "." + birth_matches.group(3)] return person personen = [] with open("Personen.txt", 'r') as file: for zeile in file: personen.append(parse_zeile(zeile)) with open("PersonenNeu.json", 'w') as file: json.dump(personen, file)