import re import json import string text_file = open(r"Personen.txt", "r") data = text_file.readlines() arr = [None] * len(data) counter = 1 text_file.close() for line in data: data_array = re.split(",", line) name = data_array[0] name_array = re.split("\s",name) adresse1 = data_array[1] adresse2 = data_array[2] geburtsdatum = data_array[3] telefon = data_array[4] telefon = telefon.replace("\n", "") titel = re.search("^.*\.$",name_array[0]) if titel != None: titel = titel.string if len(name_array) == 4: vorname = name_array[1] zweitname = name_array[2] nachname = name_array[3] else: vorname = name_array[1] nachname = name_array[2] zweitname = None else: if len(name_array) == 3: vorname = name_array[0] zweitname = name_array[1] nachname = name_array[2] else: vorname = name_array[0] nachname = name_array[1] zweitname = None # adresse_array = re.split("\s", adresse) plz = re.findall("\d{5}", adresse2) hausnummer = re.findall(r"\b\d{1,4}\b", adresse1) strasse_chars = re.findall("\D",adresse1) strasse = ''.join(strasse_chars) strasse = strasse.replace("\"", "") ort_chars = re.findall("\D",adresse2) ort = ''.join(ort_chars) ort = ort.replace("\"", "") geburtsdatum = re.findall("[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]", line) # telefon = re.findall("\d{6,20}", line) person = { "Index" : counter, "Titel" : titel, "Vorname" : vorname, "Zweitname" : zweitname, "Nachname" : nachname, "Geburtsdatum" : geburtsdatum, "Strasse" : strasse, "Hausnummer" : hausnummer, "PLZ" : plz, "Wohnort" : ort, "Rufnummer" : telefon } arr[counter - 1] = person counter = counter + 1 print(counter) with open(r"PersonenNeu.json", "w") as f: json.dump(arr,f, indent=1, ensure_ascii=False) f.close()