Upload json2txt (1).ipynb
#14
by
ACCA225
- opened
- metadata/json2txt (1).ipynb +89 -0
metadata/json2txt (1).ipynb
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": null,
|
6 |
+
"id": "74b04d07-c4df-48d9-9745-dc1aebf9b695",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [],
|
9 |
+
"source": [
|
10 |
+
"!mkdir /txt\n",
|
11 |
+
"%cd /txt\n",
|
12 |
+
"import json\n",
|
13 |
+
"from datetime import datetime\n",
|
14 |
+
"def process_line(line, count):\n",
|
15 |
+
" try:\n",
|
16 |
+
" data = json.loads(line)\n",
|
17 |
+
" # 提取关键数据转为txt\n",
|
18 |
+
" json2txt(data)\n",
|
19 |
+
" print(f'Processed line {count}')\n",
|
20 |
+
" except json.JSONDecodeError as e:\n",
|
21 |
+
" print(f\"JSONDecodeError: {e}\")\n",
|
22 |
+
"\n",
|
23 |
+
"def json2txt(data):\n",
|
24 |
+
" # 提取关键参数\n",
|
25 |
+
" id_value = str(data['id'])\n",
|
26 |
+
" tag_general = data.get('tag_string_general', '')\n",
|
27 |
+
" tag_character = data.get('tag_string_character', '')\n",
|
28 |
+
" tag_copyright = data.get('tag_string_copyright', '')\n",
|
29 |
+
" tag_artist = data.get('tag_string_artist', '')\n",
|
30 |
+
" rating = data['rating']\n",
|
31 |
+
" created_at = data['created_at']\n",
|
32 |
+
" # 格式化标签字符串\n",
|
33 |
+
" def format_tags(tag_string):\n",
|
34 |
+
" tags = tag_string.split()\n",
|
35 |
+
" formatted_tags = ', '.join(tags)\n",
|
36 |
+
" return formatted_tags\n",
|
37 |
+
" \n",
|
38 |
+
" tag_general_formatted = format_tags(tag_general)\n",
|
39 |
+
" tag_character_formatted = format_tags(tag_character)\n",
|
40 |
+
" tag_copyright_formatted = format_tags(tag_copyright)\n",
|
41 |
+
" tag_artist_formatted = format_tags(tag_artist)\n",
|
42 |
+
" \n",
|
43 |
+
" # 构造文件名\n",
|
44 |
+
" filename = f\"{id_value}.txt\"\n",
|
45 |
+
" year = datetime.strptime(created_at, \"%Y-%m-%dT%H:%M:%S.%f%z\").year\n",
|
46 |
+
" # 写入文本文件\n",
|
47 |
+
" with open(filename, 'w', encoding='utf-8') as f:\n",
|
48 |
+
" f.write(f\"{tag_artist_formatted}, \")\n",
|
49 |
+
" f.write(f\"{tag_general_formatted}, \")\n",
|
50 |
+
" f.write(f\"{tag_character_formatted}, \")\n",
|
51 |
+
" f.write(f\"{tag_copyright_formatted}, \")\n",
|
52 |
+
" f.write(f\"year_{year}, \")\n",
|
53 |
+
" if rating == 'e':\n",
|
54 |
+
" f.write(\"nsfw\")\n",
|
55 |
+
" print(f\"文件 {filename} 已成功创建并保存相关数据。\")\n",
|
56 |
+
"\n",
|
57 |
+
"# 打开原始 JSON 文件\n",
|
58 |
+
"input_file = '/posts.json' # 原始 JSON 文件路径\n",
|
59 |
+
"count = 1\n",
|
60 |
+
"\n",
|
61 |
+
"with open(input_file, 'r', encoding='utf-8') as file:\n",
|
62 |
+
" for line in file:\n",
|
63 |
+
" process_line(line, count)\n",
|
64 |
+
" count += 1\n"
|
65 |
+
]
|
66 |
+
}
|
67 |
+
],
|
68 |
+
"metadata": {
|
69 |
+
"kernelspec": {
|
70 |
+
"display_name": "Python 3 (ipykernel)",
|
71 |
+
"language": "python",
|
72 |
+
"name": "python3"
|
73 |
+
},
|
74 |
+
"language_info": {
|
75 |
+
"codemirror_mode": {
|
76 |
+
"name": "ipython",
|
77 |
+
"version": 3
|
78 |
+
},
|
79 |
+
"file_extension": ".py",
|
80 |
+
"mimetype": "text/x-python",
|
81 |
+
"name": "python",
|
82 |
+
"nbconvert_exporter": "python",
|
83 |
+
"pygments_lexer": "ipython3",
|
84 |
+
"version": "3.8.10"
|
85 |
+
}
|
86 |
+
},
|
87 |
+
"nbformat": 4,
|
88 |
+
"nbformat_minor": 5
|
89 |
+
}
|