Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.
Beide Seiten, vorherige Überarbeitung Vorherige Überarbeitung Nächste Überarbeitung | Vorherige Überarbeitung | ||
gf_informatik:daten:processing [2025-05-19 05:41] – [Aufgabe 2] hof | gf_informatik:daten:processing [2025-06-06 08:29] (aktuell) – [Aufgabe 6] hof | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
# Data Processing in Python | # Data Processing in Python | ||
+ | < | ||
{{ : | {{ : | ||
Zeile 32: | Zeile 33: | ||
#### Aufgabe 2 | #### Aufgabe 2 | ||
- | Lade die Datei [[https:// | + | Lade die Datei [[https:// |
##### Encoding | ##### Encoding | ||
Zeile 87: | Zeile 88: | ||
<nodisp 1> | <nodisp 1> | ||
++++Lösung: | ++++Lösung: | ||
- | <code python> | + | <html>< |
- | with open(' | + | |
be = 0 | be = 0 | ||
tg = 0 | tg = 0 | ||
Zeile 100: | Zeile 100: | ||
print(" | print(" | ||
print(" | print(" | ||
- | </code> | + | </bottom-editor></ |
++++ | ++++ | ||
</ | </ | ||
Zeile 113: | Zeile 114: | ||
Allerdings funktioniert das nur, wenn der String auch wirklich eine Zahl darstellt. Sonst wird eine *Exception* geworfen; wir können sie mit `try...except` auffangen: | Allerdings funktioniert das nur, wenn der String auch wirklich eine Zahl darstellt. Sonst wird eine *Exception* geworfen; wir können sie mit `try...except` auffangen: | ||
- | <code python> | + | <html>< |
try: | try: | ||
my_number = int(" | my_number = int(" | ||
Zeile 119: | Zeile 120: | ||
except ValueError: | except ValueError: | ||
pass # executed if an exception was thrown | pass # executed if an exception was thrown | ||
- | </code> | + | </bottom-editor></ |
#### Aufgabe 5 | #### Aufgabe 5 | ||
Zeile 125: | Zeile 126: | ||
* welche Spalten (Indices) gehören zu diesen Werten? | * welche Spalten (Indices) gehören zu diesen Werten? | ||
* verwendest du `int()` oder `float()`? | * verwendest du `int()` oder `float()`? | ||
- | |||
#### Aufgabe 6 | #### Aufgabe 6 | ||
Zeile 139: | Zeile 139: | ||
++++ | ++++ | ||
- | < | + | < |
++++Code:| | ++++Code:| | ||
- | <code python> | + | <html>< |
def find_smallest_population(): | def find_smallest_population(): | ||
with open(' | with open(' | ||
Zeile 161: | Zeile 161: | ||
print(" | print(" | ||
- | </code> | + | </bottom-editor></ |
++++ | ++++ | ||
</ | </ | ||
- | < | + | < |
++++Code:| | ++++Code:| | ||
- | <code python> | + | <html>< |
- | def find_smallest_population(): | + | |
- | with open('gemeinden.csv' | + | |
- | # Search for the smallest, so start out with a value larger than any expected. | + | |
- | min_pop = 1000000 | + | |
- | town = None | + | |
- | for line in towns: | + | |
- | cells = line.split(',') | + | |
- | try: | + | |
- | # Population is in the third column, and it is an integer. | + | |
- | population = int(cells[2]) | + | |
- | if population < min_pop: | + | |
- | # We found a town smaller than the currently known smallest. | + | |
- | min_pop = population | + | |
- | town = cells[0] | + | |
- | except ValueError: | + | |
- | pass | + | |
- | return town, min_pop | + | |
- | + | ||
- | print(" | + | |
def find_largest_area(): | def find_largest_area(): | ||
with open(' | with open(' | ||
Zeile 207: | Zeile 187: | ||
print(" | print(" | ||
- | </code> | + | </bottom-editor></ |
++++ | ++++ | ||
</ | </ | ||
Zeile 217: | Zeile 197: | ||
* Wieviele Gemeinden hat der Kanton Glarus? | * Wieviele Gemeinden hat der Kanton Glarus? | ||
- | < | + | < |
++++Code:| | ++++Code:| | ||
- | <code python> | + | <html>< |
def summarize_canton(canton): | def summarize_canton(canton): | ||
with open(' | with open(' | ||
Zeile 273: | Zeile 253: | ||
summarize_canton(' | summarize_canton(' | ||
summarize_canton(' | summarize_canton(' | ||
- | </code> | + | </bottom-editor></ |
++++ | ++++ | ||
</ | </ |