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:37] – [Dateien einlesen] 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 29: | Zeile 30: | ||
| * Der Typ einer Datei wird über die Dateierweiterung (`.txt`, en. _extension_) bestimmt. Windows versteckt die Endungen standardmässig, | * Der Typ einer Datei wird über die Dateierweiterung (`.txt`, en. _extension_) bestimmt. Windows versteckt die Endungen standardmässig, | ||
| * `' | * `' | ||
| - | |||
| - | |||
| #### Aufgabe 2 | #### Aufgabe 2 | ||
| Zeile 40: | Zeile 39: | ||
| <code python> | <code python> | ||
| - | # Opens a file for reading (and closes it again after the ' | + | # Specify text encoding when opening |
| with open(' | with open(' | ||
| </ | </ | ||
| Zeile 89: | Zeile 88: | ||
| <nodisp 1> | <nodisp 1> | ||
| ++++Lösung: | ++++Lösung: | ||
| - | <code python> | + | <html>< |
| - | with open(' | + | |
| be = 0 | be = 0 | ||
| tg = 0 | tg = 0 | ||
| Zeile 102: | Zeile 100: | ||
| print(" | print(" | ||
| print(" | print(" | ||
| - | </code> | + | </bottom-editor></ |
| ++++ | ++++ | ||
| </ | </ | ||
| Zeile 115: | 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 121: | 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 127: | 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 141: | Zeile 139: | ||
| ++++ | ++++ | ||
| - | < | + | < |
| ++++Code:| | ++++Code:| | ||
| - | <code python> | + | <html>< |
| def find_smallest_population(): | def find_smallest_population(): | ||
| with open(' | with open(' | ||
| Zeile 163: | 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 209: | Zeile 187: | ||
| print(" | print(" | ||
| - | </code> | + | </bottom-editor></ |
| ++++ | ++++ | ||
| </ | </ | ||
| Zeile 219: | 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 275: | Zeile 253: | ||
| summarize_canton(' | summarize_canton(' | ||
| summarize_canton(' | summarize_canton(' | ||
| - | </code> | + | </bottom-editor></ |
| ++++ | ++++ | ||
| </ | </ | ||