diff --git a/loesungen/dateien.py b/loesungen/dateien.py new file mode 100644 index 0000000..aadabfd --- /dev/null +++ b/loesungen/dateien.py @@ -0,0 +1,137 @@ +# Aufgabe 1 + +fp = open("../uebungen/employee_skills.txt", encoding="utf-8") +contents = fp.read() +fp.close() + +print("Länge:", len(contents)) + +print("-" * 50) + +# Alternativ: + +with open("../uebungen/employee_skills.txt") as fp: + contents = fp.read() + +print(contents) + +print("-" * 50) + + +# Aufgabe 2 + +with open("../uebungen/employee_skills.txt") as fp: + i = 0 + for line in fp: + line = line.strip() + + if not line: + continue + + i += 1 + +print("Nicht leere Zeilen:", i) + +print("-" * 50) + + +# Aufgabe 3 + +employees = [] +with open("../uebungen/employee_skills.txt") as fp: + for line in fp: + if not line.strip(): + continue + + name = line.split(":")[0].strip() + employees.append(name) + +employees.sort() + +for name in employees: + print(name) + +print("-" * 50) + + +# Aufgabe 4 + +employee_salaries = {} +with open("../uebungen/salaries.txt") as fp: + for line in fp: + if not line.strip(): + continue + + name, salary = line.strip().split(":", 1) + name = name.strip() + salary = int(salary.strip()) + employee_salaries[name] = salary + +print("Min. Gehalt:", min(employee_salaries.values())) +print("Max. Gehalt:", max(employee_salaries.values())) + +print("-" * 50) + + +# Aufgabe 5 + +employee_skills = {} +with open("../uebungen/employee_skills.txt") as fp: + for line in fp: + if not line.strip(): + continue + + name, skills = line.strip().split(":", 1) + name = name.strip() + skills = [item.strip() for item in skills.split(",")] + employee_skills[name] = skills + + +# Aufgabe 6 + +employees = {} + +for key in employee_salaries: + salary = employee_salaries.get(key, 0) + skills = employee_skills.get(key) + employees[key] = {"skills": skills, "salary": salary} + +print("-" * 50) + + +# Aufgabe 6 + +employee_salaries = {} +try: + with open("../uebungen/salaries-errors.txt") as fp: + for i, line in enumerate(fp): + # remove whitspace at satrt / end of line + line = line.strip() + + # ignore empty / whitespace-only lines + # and comment lines + if not line or line.startswith("#"): + continue + + try: + name, salary = line.strip().split(":", 1) + except ValueError as exc: + print(f"Warning: could not parse line {i+1}: {exc}") + else: + name = name.strip() + try: + salary = salary.strip() + salary = salary.strip("$€") + salary = int(salary) + except (ValueError, TypeError) as exc: + print(f"Warning: could not parse data on line {i+1}: {exc}") + else: + employee_salaries[name] = salary +except FileNotFoundError as exc: + print(f"Could not open file: {exc}") + + +print("Min. Gehalt:", min(employee_salaries.values())) +print("Max. Gehalt:", max(employee_salaries.values())) + +print("-" * 50)