import json import csv import re def readTitel(name): if re.match(r'^Ing\.|^Univ\.Prof\.|^Dipl\.-Ing\.',name): return re.search(r'^Ing\.|^Univ\.Prof\.|^Dipl\.-Ing\.',name).group(0) else: return None def readVorname(name): if re.match(r'^Ing\.|^Univ\.Prof\.|^Dipl\.-Ing\.', name): name = re.sub('^Ing\. |^Univ\.Prof\. |^Dipl\.-Ing\. ','',name) return re.search('(.*?) ', name).group(1) def readZweitname(name): if re.match(r'^Ing\.|^Univ\.Prof\.|^Dipl\.-Ing\.', name): name = re.sub('^Ing\. |^Univ\.Prof\. |^Dipl\.-Ing\. ','',name) if re.match(' (.*?) ',name): return re.search('(.*?) ',name) else: return None def readNachname(name): if re.match(r'^Ing\.|^Univ\.Prof\.|^Dipl\.-Ing\.', name): name = re.sub('^Ing\. |^Univ\.Prof\. |^Dipl\.-Ing\. ','',name) return re.search(' (.*)',name).group(1) def readGebDatum(gebdatum): d = re.search('(\w+$)', gebdatum).group(1) m = re.search('-(.*?)-', gebdatum).group(1) y = re.search('(.*?)-', gebdatum).group(1) return d+'.'+m+'.'+y def readStraße(adress): return re.search('(.*?) ', adress).group(1) def readHausnummer(adress): return re.search(' (.*?),', adress).group(1) def readplz(adress): return re.search(',(.*?) ', adress).group(1) def readwohnort(adress): return re.search('(?<=,(\d{5}) )[^\d](\w*[öäü]*\w*)-*(\w*[öäü]*\w*)',adress).group(0) def convert(TxtFilepath, JsonFilepath): jsonlist = [] with open(TxtFilepath, encoding='utf-8', ) as csvf: csvReader = csv.DictReader(csvf) index = 0 for rows in csvReader: data = {} data.setdefault('Index', None) data.setdefault('Titel', None) data.setdefault('Vorname', None) data.setdefault('Zweitname', None) data.setdefault('Nachname', None) data.setdefault('Geburtsdatum', None) data.setdefault('Straße', None) data.setdefault('Hausnummer', None) data.setdefault('PLZ', None) data.setdefault('Wohnort', None) name = rows['Name'] adress = rows['Adresse'] gebdatum = rows['GebDatum'] telefon = rows['Telefon'] data['Index'] = index data['Titel'] = readTitel(name) data['Vorname'] = readVorname(name) data['Zweitname'] = readZweitname(name) data['Nachname'] = readNachname(name) data['Geburtsdatum'] = readGebDatum(gebdatum) data['Straße'] = readStraße(adress) data['Hausnummer'] = readHausnummer(adress) data['PLZ'] = readplz(adress) data['Wohnort'] = readwohnort(adress) print( data['Index']) print(data['Vorname']) print(data['Zweitname']) print(data['Nachname']) index += 1 jsonlist.append(data) with open(JsonFilepath, 'w', encoding='utf-8') as jsonf: jsonf.write(json.dumps(jsonlist, indent=4, ensure_ascii=False)) txt = r'Personen.txt' JSON = r'personen.json' convert(txt, JSON)