{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Dateien"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "fp = open(\"../uebungen/salaries.txt\", encoding=\"utf-8\")\n",
    "content = fp.read()\n",
    "print(type(content))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'bytes'>\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "fp = open(\"../uebungen/salaries.txt\", \"rb\")\n",
    "content = fp.read()\n",
    "print(type(content))\n",
    "text = content.decode(\"utf-8\")\n",
    "print(type(text))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'Anna: 2000\\n'\n"
     ]
    }
   ],
   "source": [
    "fp = open(\"../uebungen/salaries.txt\", mode=\"r\")\n",
    "line = fp.readline()\n",
    "print(repr(line))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Anna: 2000\n",
      "Mark: 3000\n",
      "Judith: 3500\n",
      "Thomas: 2500\n",
      "Barbara: 3000\n",
      "Elke: 3300\n",
      "Michael: 2800\n",
      "Johann: 2000\n"
     ]
    }
   ],
   "source": [
    "fp = open(\"../uebungen/salaries.txt\", mode=\"r\", encoding=\"utf-8\")\n",
    "while True:\n",
    "    line = fp.readline()\n",
    "    if not line:\n",
    "        break\n",
    "    print(line, end=\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n",
      "['Anna: 2000\\n', 'Mark: 3000\\n', 'Judith: 3500\\n', 'Thomas: 2500\\n', 'Barbara: 3000\\n', 'Elke: 3300\\n', 'Michael: 2800\\n', 'Johann: 2000\\n']\n"
     ]
    }
   ],
   "source": [
    "fp = open(\"../uebungen/salaries.txt\", mode=\"r\", encoding=\"utf-8\")\n",
    "lines = fp.readlines()\n",
    "print(type(lines))\n",
    "print(lines)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Anna: 2000\n",
      "Mark: 3000\n",
      "Judith: 3500\n",
      "Thomas: 2500\n",
      "Barbara: 3000\n",
      "Elke: 3300\n",
      "Michael: 2800\n",
      "Johann: 2000\n"
     ]
    }
   ],
   "source": [
    "fp = open(\"../uebungen/salaries.txt\", mode=\"r\", encoding=\"utf-8\")\n",
    "for line in fp:\n",
    "    print(line, end=\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Anna: 2000\n",
      "Mark: 3000\n",
      "Judith: 3500\n",
      "Thomas: 2500\n",
      "Barbara: 3000\n",
      "Elke: 3300\n",
      "Michael: 2800\n",
      "Johann: 2000\n"
     ]
    }
   ],
   "source": [
    "fp = open(\"../uebungen/salaries.txt\", mode=\"r\", encoding=\"utf-8\")\n",
    "for line in fp:\n",
    "    print(line, end=\"\")\n",
    "\n",
    "fp.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "lines = open(\"../uebungen/salaries.txt\", encoding=\"utf-8\").readlines()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Anna: 2000\n",
      "Mark: 3000\n",
      "Judith: 3500\n",
      "Thomas: 2500\n",
      "Barbara: 3000\n",
      "Elke: 3300\n",
      "Michael: 2800\n",
      "Johann: 2000\n"
     ]
    }
   ],
   "source": [
    "with open(\"../uebungen/salaries.txt\") as fp:\n",
    "    for line in fp:\n",
    "        print(line, end=\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Pseudo Code! Nicht ausführbar\n",
    "fp = open(\"../uebungen/salaries.txt\")\n",
    "ctx = new_context(fp)\n",
    "fp.open_context(ctx)\n",
    "# code block\n",
    "fp.end_context(ctx)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Pseudo Code! Nicht ausführbar\n",
    "\n",
    "try:\n",
    "    fp = open(\"datei\")\n",
    "except FileNotFoundError:\n",
    "    # code\n",
    "except OSError:\n",
    "    # code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3]\n",
      "{\"foo\": 42, \"bar\": 23}\n",
      "{\"foo\": 42, \"bar\": 23, \"baz\": [1, 2, 3]}\n",
      "[{\"foo\": 42, \"bar\": 23}, {\"foo\": 42, \"bar\": 23, \"baz\": [1, 2, 3]}]\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "data = json.loads(\"[1, 2, 3]\")\n",
    "print(data)\n",
    "d1 = {\"foo\": 42, \"bar\": 23}\n",
    "print(json.dumps(d1))\n",
    "d2 = {\"foo\": 42, \"bar\": 23, \"baz\": [1, 2, 3]}\n",
    "print(json.dumps(d2))\n",
    "l = [d1, d2]\n",
    "print(json.dumps(l))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = {\"foo\": 42, \"bar\": 23, \"baz\": [1, 2, 3]}\n",
    "with open(\"data.json\", \"w\") as fp:\n",
    "    json.dump(data, fp)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"data.json\") as fp:\n",
    "    data2 = json.load(fp)\n",
    "\n",
    "assert data == data2, \"Not equal\""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "softed",
   "language": "python",
   "name": "softed"
  },
  "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.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
