{ "cells": [ { "cell_type": "markdown", "id": "9917ce10", "metadata": {}, "source": [ "# Datenserialisierung\n", "\n", "Wenn die Daten flach serialisiert werden sollen, bietet Python zwei Funktionen an:" ] }, { "cell_type": "markdown", "id": "fb85bcd4", "metadata": {}, "source": [ "## `repr`\n", "\n", "[repr()](https://docs.python.org/3/library/functions.html#repr) gibt eine druckbare Repräsentation der Eingabe aus, z.B.:" ] }, { "cell_type": "code", "execution_count": 1, "id": "c29d4fff", "metadata": { "execution": { "iopub.execute_input": "2026-05-22T08:23:45.765407Z", "iopub.status.busy": "2026-05-22T08:23:45.765231Z", "iopub.status.idle": "2026-05-22T08:23:45.769908Z", "shell.execute_reply": "2026-05-22T08:23:45.769479Z", "shell.execute_reply.started": "2026-05-22T08:23:45.765384Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'id': 'veit', 'first_name': 'Veit', 'last_name': 'Schiele'}\n" ] } ], "source": [ "data = {\"id\": \"veit\", \"first_name\": \"Veit\", \"last_name\": \"Schiele\"}\n", "\n", "print(repr(data))" ] }, { "cell_type": "code", "execution_count": 2, "id": "73d1b74f", "metadata": { "execution": { "iopub.execute_input": "2026-05-22T08:23:45.770820Z", "iopub.status.busy": "2026-05-22T08:23:45.770531Z", "iopub.status.idle": "2026-05-22T08:23:45.773599Z", "shell.execute_reply": "2026-05-22T08:23:45.773227Z", "shell.execute_reply.started": "2026-05-22T08:23:45.770802Z" } }, "outputs": [], "source": [ "from pathlib import Path\n", "\n", "\n", "with Path.open(\"data.txt\", \"w\") as f:\n", " f.write(repr(data))" ] }, { "cell_type": "markdown", "id": "7f66aad5", "metadata": {}, "source": [ "## `ast.literal_eval`\n", "\n", "Die [ast.literal_eval()](https://docs.python.org/3/library/ast.html#ast.literal_eval)-Funktion parst und analysiert den Python-Datentyp eines Ausdrucks. Unterstützte Datentypen sind [Zeichenketten](https://python-basics-tutorial.readthedocs.io/de/latest/types/strings.html), [Zahlen](https://python-basics-tutorial.readthedocs.io/de/latest/types/numbers.html), [Tupel](https://python-basics-tutorial.readthedocs.io/de/latest/types/tuples.html), [Listen](https://python-basics-tutorial.readthedocs.io/de/latest/types/lists.html), [Dictionaries](https://python-basics-tutorial.readthedocs.io/de/latest/types/dicts.html) und [None](https://python-basics-tutorial.readthedocs.io/de/latest/types/none.html)." ] }, { "cell_type": "code", "execution_count": 3, "id": "03647a79", "metadata": { "execution": { "iopub.execute_input": "2026-05-22T08:23:45.774493Z", "iopub.status.busy": "2026-05-22T08:23:45.774355Z", "iopub.status.idle": "2026-05-22T08:23:45.778592Z", "shell.execute_reply": "2026-05-22T08:23:45.778176Z", "shell.execute_reply.started": "2026-05-22T08:23:45.774480Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'id': 'veit', 'first_name': 'Veit', 'last_name': 'Schiele'}\n" ] } ], "source": [ "import ast\n", "\n", "\n", "with Path.open(\"data.txt\") as f:\n", " d = ast.literal_eval(f.read())\n", "\n", "print(d)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.13 Kernel", "language": "python", "name": "python313" }, "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.13.0" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }