{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import jsonlines\n", "\n", "\n", "#步骤1:读取JSON文件\n", "def read_json(file_path):\n", " data = []\n", " with open(file_path, \"r+\", encoding=\"utf8\") as f:\n", " for item in jsonlines.Reader(f):\n", " data.append(item)\n", " return data\n", "\n", "file_path = '/mnt/sda5/llm_data/ABear___Poetry_CN/gushiju-net-266k.jsonl'\n", "data = read_json(file_path)\n", "print(\"數據總筆數:\", len(data))\n", "print(data[0])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 查看字段名\n", "if isinstance(data, dict):\n", " print(data.keys())\n", "elif isinstance(data, list) and len(data) > 0 and isinstance(data[0], dict):\n", " print(data[0].keys())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "item = data[0]\n", "print(item['title'])\n", "print(item['shici_text'])\n", "print(item['fanyi'])\n", "print(item['zhushi'])\n", "\n", "print(item['source_name'])\n", "print(item['chaodai'])\n", "print(item['zuozhe'])\n", "#print(item['zuozhe_info'])\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"譯文及註釋:\" + list(item.get(\"content_json\").values())[0] + \"。\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"賞析:\" + list(item.get(\"content_json\").values())[1]+ \"。\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"創作背景:\" + list(item.get(\"content_json\").values())[2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(item['fanyi'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "gpt", "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.14" } }, "nbformat": 4, "nbformat_minor": 2 }