Teile 10 bis 12.

This commit is contained in:
2023-03-28 12:26:50 +02:00
parent 298151634d
commit f7a5db9f39
27 changed files with 6459 additions and 75 deletions

Binary file not shown.

5
teil10/namen.csv Normal file
View File

@@ -0,0 +1,5 @@
Skinner,Seymour
Largo,Dewey
Krabappel,Edna
Hoover,Elisabeth
Chalmers,Gary
1 Skinner Seymour
2 Largo Dewey
3 Krabappel Edna
4 Hoover Elisabeth
5 Chalmers Gary

1
teil10/namen.py Normal file
View File

@@ -0,0 +1 @@
namen = ["Homer", "Marge", "Bart"]

3
teil10/namen.txt Normal file
View File

@@ -0,0 +1,3 @@
Homer
Marge
Bart

29
teil10/read_csv.py Normal file
View File

@@ -0,0 +1,29 @@
school = []
class Lehrer():
def __init__(self, vorname, name):
self.vorname = vorname
self.name = name
def __str__(self):
return f'{self.vorname} {self.name}'
try:
with open('namen.csv','r') as f:
for line in f:
splitted = line.strip().split(',')
name = splitted[0]
vorname = splitted[1]
lehrer = Lehrer(vorname,name)
school.append(lehrer)
except IOError as x:
print(f'I/O-Fehler: {x}')
print(school)
print(school[0])
print(school[3])

17
teil10/read_file.py Normal file
View File

@@ -0,0 +1,17 @@
namen = []
try:
f = open('namen.txt','r')
#f = open('namen.txt','r',encoding='utf-8')
for line in f:
namen.append(line)
# namen.append(line.strip())
except IOError as x:
print(f'I/O-Fehler: {x}')
finally:
if f != None:
f.close()
print(namen)

15
teil10/write_file.py Normal file
View File

@@ -0,0 +1,15 @@
from namen import namen
f = open('namen.txt','w')
try:
for name in namen:
f.write(f'{name}\n')
except IOError as err:
print(f'I/O-Fehler {err}')
finally:
if f != None:
f.close()

12
teil10/write_file_with.py Normal file
View File

@@ -0,0 +1,12 @@
from namen import namen
try:
with open('namen.txt','w') as f:
for name in namen:
f.write(f'{name}\n')
except IOError as err:
print(f'I/O-Fehler {err}')