- Computer Grundlagen II: Datenkodierung
- Python Grundlagen II: Datenstrukturen
- Projekt 2: CSV & JSON
- Quellen zum Selbststudium
>>> for i in range(1,10):
... print(i)
...
Python 3.10.6
>>> import sys
>>> sys.float_info
sys.float_info(
max=1.7976931348623157e+308, ...
min=2.2250738585072014e-308, ...)
>>> a = 5
>>> type(a)
<class 'int'>
>>> b = 3.14
>>> type(b)
<class 'float'>
>>> z = complex(3,4)
>>> type(z)
<class 'complex'>
>>> z.real
3.0
>>> z.imag
4.0
Python 2.7.18
>>> import sys
>>> sys.maxint
9223372036854775807
>>> list = [5, 3, "Hallo"]
>>> list
[5, 3, 'Hallo']
>>> list[0]
5
>>> list[2]
'Hallo'
>>> list.append(3)
>>> list
[5, 3, 'Hallo', 3]
>>> tuple = (1, 3, "Welt")
>>> tuple
(1, 3, 'Welt')
>>> tuple[2]
'Welt'
>>> x, y, z = tuple
>>> x
1
>>> y
3
>>> z
'Welt'
>>> dict = {}
>>> type(dict)
>>> dict['Hallo'] = 'Welt'
>>> dict
{'Hallo': 'Welt'}
>>> list
[5, 3, 'Hallo', 3]
>>> set = set(list)
>>> set
{'Hallo', 3, 5}
>>> with open('cap_warncellids_csv.csv', 'r') as f:
... print(f.readline())
...
# WARNCELLID;NAME;KENNUNG (NUTS);KURZNAME;KENNUNG (SIGN)
>>> import csv
>>> with open('cap_warncellids_csv.csv', newline='') as csvfile:
... reader = csv.reader(csvfile, delimiter=';', quotechar='"')
... for row in reader:
... print(row)
...
['\ufeff# WARNCELLID', 'NAME', 'KENNUNG (NUTS)', 'KURZNAME', 'KENNUNG (SIGN)']
['101001000', 'Stadt Flensburg', 'DEF01', 'Flensburg', 'FLX']
['101002000', 'Stadt Kiel', 'DEF02', 'Kiel', 'KIX']
>>> namen = ['Paul', 'Peter', 'Gustav']
>>> for name in namen:
... if name == 'Paul':
... print('Paul wurde gefunden!')
... elif 'P' in name:
... print('Gefunden:', name)
... else:
... print('Nicht gefunden:', name)
...
Paul wurde gefunden!
Gefunden: Peter
Nicht gefunden: Gustav
import csv
with open('cap_warncellids_csv.csv', newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=';', quotechar='"')
for row in reader:
if row[3] == 'Roth':
print(f'{row[1]}: {row[0]}')
> python teil1.py
Kreis Roth: 109576000
Stadt Roth: 809576143
>>> namen = ['Paul', 'Peter', 'Gustav']
>>> for i, name in enumerate(namen):
... print(i, name)
...
0 Paul
1 Peter
2 Gustav
>>> liste = [1,2,3,4,5,6,7,8,9]
>>> liste[:5]
[1, 2, 3, 4, 5]
>>> liste[5:]
[6, 7, 8, 9]
>>> liste[1:6:2]
[2, 4, 6]
>>> liste[::2]
[1, 3, 5, 7, 9]
import csv
import matplotlib.pyplot as plt
with open('weather_data.csv', 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='"')
spalten = reader.__next__()
# Finde Spalten-Nr
index = None
for i, titel in enumerate(spalten):
if titel == 'DE_temperature':
index = i
print("Die Daten sind in Spalte", index)
# Extrahiere Daten
zeit = []
temp = []
for row in reader:
zeit.append(row[0][:4])
temp.append(float(row[index]))
print("Die Daten sind extrahiert.")
...
...
# Daten Darstellen
fig, ax = plt.subplots()
ax.plot(zeit[::8760], temp[::8760])
plt.show()
>>> import json
>>> data = None
>>> with open('data.json', 'r') as f:
... data = json.load(f)
...
>>> print(data['data'][0])
{'ID Nation': '01000US', 'Nation': 'United States', 'ID Year': 2020, 'Year': '2020', 'Population': 326569308, 'Slug Nation': 'united-states'}
import json
import matplotlib.pyplot as plt
with open('data.json', 'r') as f:
data = json.load(f)
# Extrahiere Daten
jahr = []
anzahl = []
for eintrag in data['data']:
jahr.append(eintrag['Year'])
anzahl.append(int(eintrag['Population']))
print("Die Daten sind extrahiert.")
# Daten Darstellen
jahr.reverse()
anzahl.reverse()
fig, ax = plt.subplots()
ax.plot(jahr, anzahl)
plt.show()