# %% [markdown] # # Dateien # %% fp = open("../uebungen/salaries.txt", encoding="utf-8") content = fp.read() print(type(content)) # %% fp = open("../uebungen/salaries.txt", "rb") content = fp.read() print(type(content)) text = content.decode("utf-8") print(type(text)) # %% fp = open("../uebungen/salaries.txt", mode="r") line = fp.readline() print(repr(line)) # %% fp = open("../uebungen/salaries.txt", mode="r", encoding="utf-8") while True: line = fp.readline() if not line: break print(line, end="") # %% fp = open("../uebungen/salaries.txt", mode="r", encoding="utf-8") lines = fp.readlines() print(type(lines)) print(lines) # %% fp = open("../uebungen/salaries.txt", mode="r", encoding="utf-8") for line in fp: print(line, end="") # %% fp = open("../uebungen/salaries.txt", mode="r", encoding="utf-8") for line in fp: print(line, end="") fp.close() # %% lines = open("../uebungen/salaries.txt", encoding="utf-8").readlines() # %% with open("../uebungen/salaries.txt") as fp: for line in fp: print(line, end="") # %% # Pseudo Code! Nicht ausführbar fp = open("../uebungen/salaries.txt") ctx = new_context(fp) fp.open_context(ctx) # code block fp.end_context(ctx) # %% # Pseudo Code! Nicht ausführbar try: fp = open("datei") except FileNotFoundError: # code except OSError: # code # %% import json data = json.loads("[1, 2, 3]") print(data) d1 = {"foo": 42, "bar": 23} print(json.dumps(d1)) d2 = {"foo": 42, "bar": 23, "baz": [1, 2, 3]} print(json.dumps(d2)) l = [d1, d2] print(json.dumps(l)) # %% data = {"foo": 42, "bar": 23, "baz": [1, 2, 3]} with open("data.json", "w") as fp: json.dump(data, fp) # %% with open("data.json") as fp: data2 = json.load(fp) assert data == data2, "Not equal"