{ "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", " | Titel | \n", "Sprache | \n", "Autor*innen | \n", "Lizenz | \n", "Veröffentlichungsdatum | \n", "
|---|---|---|---|---|---|
| 0 | \n", "Python basics | \n", "en | \n", "Veit Schiele | \n", "BSD-3-Clause | \n", "2021-10-28 | \n", "
| 1 | \n", "Jupyter Tutorial | \n", "en | \n", "Veit Schiele | \n", "BSD-3-Clause | \n", "2019-06-27 | \n", "
| 2 | \n", "Jupyter Tutorial | \n", "de | \n", "Veit Schiele | \n", "BSD-3-Clause | \n", "2020-10-26 | \n", "
| 3 | \n", "PyViz Tutorial | \n", "en | \n", "Veit Schiele | \n", "BSD-3-Clause | \n", "2020-04-13 | \n", "