{ "cells": [ { "cell_type": "markdown", "id": "b0a67390", "metadata": {}, "source": [ "# Pickle-Beispiele" ] }, { "cell_type": "markdown", "id": "1e3f1486", "metadata": {}, "source": [ "## Python-`pickle`-Modul\n", "\n", "In diesem Beispiel wollen wir mit dem Python [pickle](https://docs.python.org/3/library/pickle.html)-Modul das foldende Dict im Pickle-Format speichern:" ] }, { "cell_type": "code", "execution_count": 1, "id": "4df6709f", "metadata": { "execution": { "iopub.execute_input": "2026-05-22T08:26:06.583159Z", "iopub.status.busy": "2026-05-22T08:26:06.582745Z", "iopub.status.idle": "2026-05-22T08:26:06.586455Z", "shell.execute_reply": "2026-05-22T08:26:06.585916Z", "shell.execute_reply.started": "2026-05-22T08:26:06.583140Z" } }, "outputs": [], "source": [ "pyviz = {\n", " \"Titel\": \"PyViz Tutorial\",\n", " \"Sprache\": \"de\",\n", " \"Autor*innen\": \"Veit Schiele\",\n", " \"Lizenz\": \"BSD-3-Clause\",\n", " \"Veröffentlichungsdatum\": \"2020-04-13\",\n", "}" ] }, { "cell_type": "code", "execution_count": 2, "id": "a1b0ed14", "metadata": { "execution": { "iopub.execute_input": "2026-05-22T08:26:06.587130Z", "iopub.status.busy": "2026-05-22T08:26:06.586985Z", "iopub.status.idle": "2026-05-22T08:26:06.589364Z", "shell.execute_reply": "2026-05-22T08:26:06.588905Z", "shell.execute_reply.started": "2026-05-22T08:26:06.587115Z" } }, "outputs": [], "source": [ "import pickle\n", "\n", "from pathlib import Path" ] }, { "cell_type": "code", "execution_count": 3, "id": "2504a8a1", "metadata": { "execution": { "iopub.execute_input": "2026-05-22T08:26:06.590061Z", "iopub.status.busy": "2026-05-22T08:26:06.589909Z", "iopub.status.idle": "2026-05-22T08:26:06.593095Z", "shell.execute_reply": "2026-05-22T08:26:06.592772Z", "shell.execute_reply.started": "2026-05-22T08:26:06.590038Z" } }, "outputs": [], "source": [ "with Path.open(\"pyviz.pkl\", \"wb\") as f:\n", " pickle.dump(pyviz, f, pickle.HIGHEST_PROTOCOL)" ] }, { "cell_type": "markdown", "id": "678c6444", "metadata": {}, "source": [ "Nun lesen wir die Pickle-Datei wieder ein:" ] }, { "cell_type": "code", "execution_count": 4, "id": "209a46ed", "metadata": { "execution": { "iopub.execute_input": "2026-05-22T08:26:06.593746Z", "iopub.status.busy": "2026-05-22T08:26:06.593643Z", "iopub.status.idle": "2026-05-22T08:26:06.596732Z", "shell.execute_reply": "2026-05-22T08:26:06.595911Z", "shell.execute_reply.started": "2026-05-22T08:26:06.593736Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'Titel': 'PyViz Tutorial', 'Sprache': 'de', 'Autor*innen': 'Veit Schiele', 'Lizenz': 'BSD-3-Clause', 'Veröffentlichungsdatum': '2020-04-13'}\n" ] } ], "source": [ "with Path.open(\"pyviz.pkl\", \"rb\") as f:\n", " pyviz = pickle.load(f)\n", "\n", "print(pyviz)" ] }, { "cell_type": "markdown", "id": "02b84446", "metadata": {}, "source": [ "Auf diese Weise können wir Python-Objekte einfach persistent speichern.\n", "\n", "
\n", "\n", "**Warnung**\n", "\n", "`pickle` kann nur als kurzfristiges Speicherformat empfohlen werden. Das Problem ist, dass nicht garantiert wird, dass das Format im Laufe der Zeit stabil bleibt; ein heute gepickeltes Objekt lässt sich möglicherweise mit einer späteren Version der Bibliothek nicht mehr entpickeln.\n", "
\n", "
\n", "\n", "**Warnung**\n", "\n", "`pickle` und Module, die darauf aufbauen, können bei der Deserialisierung nicht vertrauenswürdiger Daten Sicherheitsrisiken bergen.\n", "
" ] }, { "cell_type": "markdown", "id": "492079d2", "metadata": {}, "source": [ "## pandas\n", "\n", "Alle Pandas-Objekte haben eine `to_pickle`-Methode, die Daten im Pickle-Format auf die Festplatte schreibt:" ] }, { "cell_type": "code", "execution_count": 5, "id": "3e8c1438", "metadata": { "execution": { "iopub.execute_input": "2026-05-22T08:26:06.597450Z", "iopub.status.busy": "2026-05-22T08:26:06.597362Z", "iopub.status.idle": "2026-05-22T08:26:06.815452Z", "shell.execute_reply": "2026-05-22T08:26:06.815120Z", "shell.execute_reply.started": "2026-05-22T08:26:06.597440Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
TitelSpracheAutor*innenLizenzVeröffentlichungsdatum
0Python basicsenVeit SchieleBSD-3-Clause2021-10-28
1Jupyter TutorialenVeit SchieleBSD-3-Clause2019-06-27
2Jupyter TutorialdeVeit SchieleBSD-3-Clause2020-10-26
3PyViz TutorialenVeit SchieleBSD-3-Clause2020-04-13
\n", "
" ], "text/plain": [ " Titel Sprache Autor*innen Lizenz Veröffentlichungsdatum\n", "0 Python basics en Veit Schiele BSD-3-Clause 2021-10-28\n", "1 Jupyter Tutorial en Veit Schiele BSD-3-Clause 2019-06-27\n", "2 Jupyter Tutorial de Veit Schiele BSD-3-Clause 2020-10-26\n", "3 PyViz Tutorial en Veit Schiele BSD-3-Clause 2020-04-13" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "\n", "books = pd.read_pickle(\"books.pkl\")\n", "\n", "books" ] }, { "cell_type": "markdown", "id": "65d0c640", "metadata": {}, "source": [ "pandas-Objekte haben alle eine `to_pickle`-Methode, die die Daten im Pickle-Format auf die Festplatte schreibt:" ] }, { "cell_type": "code", "execution_count": 6, "id": "cb529da7", "metadata": { "execution": { "iopub.execute_input": "2026-05-22T08:26:06.817600Z", "iopub.status.busy": "2026-05-22T08:26:06.817447Z", "iopub.status.idle": "2026-05-22T08:26:06.819747Z", "shell.execute_reply": "2026-05-22T08:26:06.819506Z", "shell.execute_reply.started": "2026-05-22T08:26:06.817588Z" } }, "outputs": [], "source": [ "books.to_pickle(\"books.pkl\")" ] } ], "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 }