a3 überarbeitet, Zeile 290 einlesen funktioniert, ohne try:, mehr regex

Main
kevin 2023-01-18 19:21:09 +01:00
parent fc34a84e3b
commit dbb2f8044b
1 changed files with 81 additions and 81 deletions

View File

@ -1,82 +1,82 @@
import re import re
import json import json
import string
file = open(r"Personen.txt", 'r')
text_file = open(r"Personen.txt", "r") lines = file.readlines()
data = text_file.readlines() del lines[0] #delete line "Name,Adresse,GebDatum,Telefon"
arr = [None] * len(data) personsData = [None] * len(lines)
counter = 1
text_file.close() file.close()
for line in data: index = 0 #set index equals line
try:
data_array = re.split(",", line) for line in lines:
name = data_array[0] lineData = re.split(",", line) #splits whole line into name, adresse, birthday, number
name_array = re.split("\s",name) nameData = re.split("\s", lineData[0]) #splits name area
adresse1 = data_array[1]
adresse2 = data_array[2] #pre inzialiizing data
geburtsdatum = data_array[3] title = None #could be null
telefon = data_array[4] vname = ""
telefon = telefon.replace("\n", "") secName = None #could be null
name = ""
titel = re.search("^.*\.$",name_array[0]) bday = ""
street = ""
streetNumber = 0 #integer-format
if titel != None: postal = 0 #integer-format
titel = titel.string city = ""
if len(name_array) == 4: phone = ""
vorname = name_array[1]
zweitname = name_array[2] #Name-Extract
nachname = name_array[3] if(re.fullmatch(".*\.$", nameData[0]) and len(nameData) > 2): #Ausnahmebehandlung für "Klaus-D. Baum" Zeile 290 bzw. Personen . im Vornamen | . = beliebiger char \.$ = Ende auf Punkt
else: title = nameData[0]
vorname = name_array[1] if(len(nameData) == 4): #Check für Zweitnamen
nachname = name_array[2] vname = nameData[1]
zweitname = None secName = nameData[2]
else: name = nameData[3]
if len(name_array) == 3: else:
vorname = name_array[0] vname = nameData[1]
zweitname = name_array[1] name = nameData[2]
nachname = name_array[2] else:
else: if(len(nameData) == 3): #Check für Zweitnamen
vorname = name_array[0] vname = nameData[0]
nachname = name_array[1] secName = nameData[1]
zweitname = None name = nameData[2]
else:
# adresse_array = re.split("\s", adresse) vname = nameData[0]
plz = re.findall("\d{5}", adresse2) name = nameData[1]
hausnummer = re.findall(r"\b\d{1,4}\b", adresse1)
strasse_chars = re.findall("\D",adresse1) #Adresse-Extract
strasse = ''.join(strasse_chars) street = re.search('[^\"\s\d]+', lineData[1]).group(0) # ^ = ignoriert Leerzeichen " und Zahlen
strasse = strasse.replace("\"", "") streetNumber = re.search('(?<=\D)\d+', lineData[1]).group(0) #beginnt bei ersten Zahl
ort_chars = re.findall("\D",adresse2) postal = re.search('\d{5}', lineData[2]).group(0) #extrahiert die ersten fünf Zahlen
ort = ''.join(ort_chars) city = re.search('[^\"\s\d]+', lineData[2]).group(0) #ignoriert " Leerzeichen und Zahlen
ort = ort.replace("\"", "")
#Birthday
bdayData = re.split("-", lineData[3]) #split birthday data
geburtsdatum = re.findall("[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]", line) bday = bdayData[2]+"."+bdayData[1]+"."+bdayData[0] #compose data in right order
# telefon = re.findall("\d{6,20}", line)
#Telefon
person = { phone = re.search('[^\n]+', lineData[4]).group(0) #just extract \n and keep format
"Index" : counter,
"Titel" : titel, #json format einteilung
"Vorname" : vorname, person = {
"Zweitname" : zweitname, 'Index' : index,
"Nachname" : nachname, 'Titel' : title,
"Geburtsdatum" : geburtsdatum, 'Vorname' : vname,
"Strasse" : strasse, 'Zweitname' : secName,
"Hausnummer" : hausnummer, 'Nachname' : name,
"PLZ" : plz, 'Geburtstag' : bday,
"Wohnort" : ort, 'Strasse' : street,
"Rufnummer" : telefon 'Hausnummer' : streetNumber,
} 'PLZ' : postal,
'Wohnort' : city,
arr[counter - 1] = person 'Telefonnummer' : phone}
counter = counter + 1 personsData[index] = person
except:
continue index += 1
with open(r"PersonenNeu.json", "w") as f: with open(r'PersonenNeu.json', 'w') as f:
json.dump(arr,f, indent=1, ensure_ascii=False) json.dump(personsData, f, indent=1, ensure_ascii=False)
f.close() f.close()