Compare commits

...

2 Commits

Author SHA1 Message Date
Christopher Arndt dd36861233 Add solutions for uebungen for files
Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
2024-05-08 16:44:42 +02:00
Christopher Arndt 205c83790c Update beispiele
Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
2024-05-08 16:20:55 +02:00
3 changed files with 175 additions and 2 deletions

34
beispiele/http1.py Normal file
View File

@ -0,0 +1,34 @@
import sys
import requests
import lxml.html
base_url = "https://en.wikipedia.org/"
if len(sys.argv) >= 2:
url = base_url + sys.argv[1]
else:
url = "https://en.wikipedia.org/wiki/Python_(programming_language)"
resp = requests.get(url)
see_also = []
if resp.status_code == 200:
html = resp.text
#print(html[:100])
tree = lxml.html.fromstring(html)
see_also_heading = tree.xpath("//span[@id='See_also']")[0].getparent()
for sibling in see_also_heading.itersiblings():
hrefs = sibling.xpath("li/a")
for href in hrefs:
title = href.get("title")
see_also_url = href.get("href")
#print(title, see_also_url)
## TODO
## Parse URL from command line with urlllib.parse.urlsplit()
## and concatenate base URL and relative URL from link
see_also.append((title, base_url + see_also_url))
print(see_also)

View File

@ -24,11 +24,13 @@ try:
print(f"Warning: could not parse line {i+1}: {exc}") print(f"Warning: could not parse line {i+1}: {exc}")
else: else:
try: try:
items = [int(item) for item in raw_data.split(",")] # split raw data at commas # split raw data at commas
items = [int(item.strip()) for item in raw_data.split(",")]
except (ValueError, TypeError) as exc: except (ValueError, TypeError) as exc:
print(f"Warning: could not parse data on line {i+1}: {exc}") print(f"Warning: could not parse data on line {i+1}: {exc}")
else:
data[name.strip()] = items
data[name.strip()] = items
except OSError as exc: except OSError as exc:
print(f"Could not open file {filename}: {exc}") print(f"Could not open file {filename}: {exc}")

137
loesungen/dateien.py Normal file
View File

@ -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)