191 lines
4.1 KiB
Plaintext
191 lines
4.1 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Hello World\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"file_object = open(\"myfile.txt\", \"r\")\n",
|
|
"\n",
|
|
"line = file_object.readline()\n",
|
|
"\n",
|
|
"print(line)\n",
|
|
"file_object.close()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Hello World\n",
|
|
"Bye Jupiter\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Öffnen mit Kontextmanager\n",
|
|
"\n",
|
|
"with open(\"myfile.txt\", \"r\") as file_object:\n",
|
|
" # entfernen von newlines\n",
|
|
" lines = [line.strip() for line in file_object.readlines()]\n",
|
|
" for line in lines:\n",
|
|
" print(line)\n",
|
|
" # automatisches schließen"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"62696e616572646174656e\n",
|
|
"b'binaerdaten'\n",
|
|
"62696e616572646174656e\n",
|
|
"bytearray(b'binaerdaten')\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Binärdaten in Python\n",
|
|
"\n",
|
|
"\"\"\"\n",
|
|
"Bytes:\n",
|
|
" - Literale enthalten nur ASCII-Zeichen\n",
|
|
" - unveränderlich\n",
|
|
"\"\"\"\n",
|
|
"\n",
|
|
"b = b\"binaerdaten\"\n",
|
|
"print(b.hex())\n",
|
|
"print(b)\n",
|
|
"\n",
|
|
"\"\"\"\n",
|
|
"Bytearray:\n",
|
|
" - veränderlich\n",
|
|
"\"\"\"\n",
|
|
"\n",
|
|
"ba = bytearray(b)\n",
|
|
"print(ba.hex())\n",
|
|
"print(ba)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 13,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"0\n",
|
|
"50\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"name = \"PR3 Teil Skriptsprachen\"\n",
|
|
"\n",
|
|
"with open(\"hello.txt\", \"w\") as f:\n",
|
|
" f.truncate()\n",
|
|
" print(f.tell())\n",
|
|
" f.write(\"Hello, {}!\\n\".format(name))\n",
|
|
" f.write(\"Genug der Tollerei\")\n",
|
|
" print(f.tell())"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 14,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"with open(\"accounts.txt\", mode=\"w\") as f:\n",
|
|
" print(\"100 Mali 24.98\", file=f)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 29,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Account Name Balance \n",
|
|
"100 Jones 24.98 \n",
|
|
"200 Doe 345.67 \n",
|
|
"300 White 0.0 \n",
|
|
"400 Stone -42.16 \n",
|
|
"500 Rich 224.62 \n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Aufgabe\n",
|
|
"\n",
|
|
"accounts = list(range(100,600,100))\n",
|
|
"names = [\"Jones\", \"Doe\", \"White\", \"Stone\", \"Rich\"]\n",
|
|
"balances = [24.98, 345.67, 0.00, -42.16, 224.62]\n",
|
|
"\n",
|
|
"# Erzeugen von buchungen.txt\n",
|
|
"with open(\"buchungen.txt\", \"w\") as file:\n",
|
|
" for i in range(5):\n",
|
|
" file.write(f\"{accounts[i]} {names[i]} {balances[i]}\\n\")\n",
|
|
"\n",
|
|
"\n",
|
|
"# Formatierte Ausgabe von Buchungen.txt\n",
|
|
"with open(\"buchungen.txt\", \"r\") as file:\n",
|
|
" lines = [line.strip() for line in file.readlines()]\n",
|
|
"\n",
|
|
" print(f\"{'Account':<8} {'Name':<8} {'Balance':<8}\")\n",
|
|
" for line in lines:\n",
|
|
" entry = line.split(\" \")\n",
|
|
" entry = (int(entry[0]), entry[1], float(entry[2]))\n",
|
|
" print(f\"{entry[0]:<8} {entry[1]:<8} {entry[2]:<8}\")"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "base",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.10.9"
|
|
},
|
|
"orig_nbformat": 4
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|