ACCA225 commited on
Commit
977ee1a
1 Parent(s): 4ddd8c6

Upload json_extract.ipynb

Browse files
Files changed (1) hide show
  1. metadata/json_extract.ipynb +234 -0
metadata/json_extract.ipynb ADDED
@@ -0,0 +1,234 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "273f0de8-f2ec-4147-ab8e-cccf6ca93d86",
6
+ "metadata": {},
7
+ "source": [
8
+ "# 提取posts.json"
9
+ ]
10
+ },
11
+ {
12
+ "cell_type": "code",
13
+ "execution_count": null,
14
+ "id": "5b970c9d-38dc-4865-86cd-c4583d3619be",
15
+ "metadata": {},
16
+ "outputs": [],
17
+ "source": [
18
+ "import json\n",
19
+ "\n",
20
+ "def process_line(line, count):\n",
21
+ " try:\n",
22
+ " data = json.loads(line)\n",
23
+ " # 在这里添加处理每个 JSON 对象的代码\n",
24
+ "\n",
25
+ " # 创建新的 JSON 文件名,例如 new_posts_000001.json\n",
26
+ " new_file_name = f'/json/new_posts_{count:06d}.json'\n",
27
+ " with open(new_file_name, 'w', encoding='utf-8') as new_file:\n",
28
+ " json.dump(data, new_file, ensure_ascii=False, indent=4) # 写入 JSON 数据到新文件\n",
29
+ " print(f'Processed line {count}: {new_file_name}')\n",
30
+ " except json.JSONDecodeError as e:\n",
31
+ " print(f\"JSONDecodeError: {e}\")\n",
32
+ "\n",
33
+ "# 打开原始 JSON 文件\n",
34
+ "input_file = '/posts.json' # 原始 JSON 文件路径\n",
35
+ "count = 1\n",
36
+ "\n",
37
+ "with open(input_file, 'r', encoding='utf-8') as file:\n",
38
+ " for line in file:\n",
39
+ " process_line(line, count)\n",
40
+ " count += 1\n"
41
+ ]
42
+ },
43
+ {
44
+ "cell_type": "code",
45
+ "execution_count": null,
46
+ "id": "8834f932-775a-4649-9b5c-98c2972e16ef",
47
+ "metadata": {},
48
+ "outputs": [],
49
+ "source": [
50
+ "#查看拆出的json文件\n",
51
+ "!cp json/new_posts_000001.json ./"
52
+ ]
53
+ },
54
+ {
55
+ "cell_type": "markdown",
56
+ "id": "fcd9735c-1b27-4471-be68-8122ebc8b08d",
57
+ "metadata": {},
58
+ "source": [
59
+ "# 提取json关键数据到txt文件,以id为命名"
60
+ ]
61
+ },
62
+ {
63
+ "cell_type": "code",
64
+ "execution_count": null,
65
+ "id": "301ec542-9117-4e15-b356-85d4cfcb4662",
66
+ "metadata": {},
67
+ "outputs": [],
68
+ "source": [
69
+ "import json\n",
70
+ "\n",
71
+ "# 文件路径\n",
72
+ "file_path = 'json/new_posts_000001.json'\n",
73
+ "\n",
74
+ "# 读取JSON数据\n",
75
+ "with open(file_path, 'r', encoding='utf-8') as file:\n",
76
+ " data = json.load(file)\n",
77
+ "\n",
78
+ "# 提取关键参数\n",
79
+ "id_value = str(data['id'])\n",
80
+ "tag_general = data['tag_string_general']\n",
81
+ "tag_character = data['tag_string_character']\n",
82
+ "tag_copyright = data['tag_string_copyright']\n",
83
+ "tag_artist = data['tag_string_artist']\n",
84
+ "\n",
85
+ "# 格式化标签字符串\n",
86
+ "def format_tags(tag_string):\n",
87
+ " tags = tag_string.split()\n",
88
+ " formatted_tags = ', '.join(tags)\n",
89
+ " return formatted_tags\n",
90
+ "\n",
91
+ "tag_general_formatted = format_tags(tag_general)\n",
92
+ "tag_character_formatted = format_tags(tag_character)\n",
93
+ "tag_copyright_formatted = format_tags(tag_copyright)\n",
94
+ "tag_artist_formatted = format_tags(tag_artist)\n",
95
+ "\n",
96
+ "# 构造文件名\n",
97
+ "filename = f\"{id_value}.txt\"\n",
98
+ "\n",
99
+ "# 写入文本文件\n",
100
+ "with open(filename, 'w', encoding='utf-8') as f:\n",
101
+ " f.write(f\"{tag_artist_formatted}, \")\n",
102
+ " f.write(f\"{tag_general_formatted}, \")\n",
103
+ " f.write(f\"{tag_character_formatted}, \")\n",
104
+ " f.write(f\"{tag_copyright_formatted}\")\n",
105
+ " \n",
106
+ "\n",
107
+ "print(f\"文件 {filename} 已成功创建并保存相关数据。\")\n",
108
+ "\n",
109
+ "# 如果你想批量提取,改成循环迭代吧,不会改的让chatgpt改就行\n"
110
+ ]
111
+ },
112
+ {
113
+ "cell_type": "raw",
114
+ "id": "879921e1-3981-4ad4-a60a-5b4cbb60a70e",
115
+ "metadata": {},
116
+ "source": [
117
+ "posts.json的数据可以参考如下:\n",
118
+ "{\n",
119
+ " \"id\": 534268,\n",
120
+ " \"created_at\": \"2009-10-02T01:54:46.660-04:00\",\n",
121
+ " \"uploader_id\": 102191,\n",
122
+ " \"score\": 3,\n",
123
+ " \"source\": \"https://i.pximg.net/img-original/img/2009/10/02/18/53/34/6451237_p0.jpg\",\n",
124
+ " \"md5\": \"21fa0fed0c5acd15cbf4f44018466649\",\n",
125
+ " \"last_comment_bumped_at\": \"2011-06-02T20:27:37.439-04:00\",\n",
126
+ " \"rating\": \"s\",\n",
127
+ " \"image_width\": 621,\n",
128
+ " \"image_height\": 1839,\n",
129
+ " \"tag_string\": \"4koma 6+girls check_translation comic commentary_request danmaku dei_shirou highres komeiji_satori kurodani_yamame mizuhashi_parsee multiple_girls reiuji_utsuho saigyouji_yuyuko touhou touhou_tag_dream translated translation_request yasaka_kanako\",\n",
130
+ " \"fav_count\": 8,\n",
131
+ " \"file_ext\": \"jpg\",\n",
132
+ " \"last_noted_at\": \"2022-05-20T19:58:43.212-04:00\",\n",
133
+ " \"parent_id\": null,\n",
134
+ " \"has_children\": false,\n",
135
+ " \"approver_id\": null,\n",
136
+ " \"tag_count_general\": 5,\n",
137
+ " \"tag_count_artist\": 1,\n",
138
+ " \"tag_count_character\": 6,\n",
139
+ " \"tag_count_copyright\": 2,\n",
140
+ " \"file_size\": 363768,\n",
141
+ " \"up_score\": 3,\n",
142
+ " \"down_score\": 0,\n",
143
+ " \"is_pending\": false,\n",
144
+ " \"is_flagged\": false,\n",
145
+ " \"is_deleted\": false,\n",
146
+ " \"tag_count\": 19,\n",
147
+ " \"updated_at\": \"2022-04-02T14:13:05.372-04:00\",\n",
148
+ " \"is_banned\": false,\n",
149
+ " \"pixiv_id\": 6451237,\n",
150
+ " \"last_commented_at\": \"2012-06-14T09:12:31.388-04:00\",\n",
151
+ " \"has_active_children\": false,\n",
152
+ " \"bit_flags\": 0,\n",
153
+ " \"tag_count_meta\": 5,\n",
154
+ " \"has_large\": false,\n",
155
+ " \"has_visible_children\": false,\n",
156
+ " \"media_asset\": {\n",
157
+ " \"id\": 512269,\n",
158
+ " \"created_at\": \"2009-10-02T01:54:46.660-04:00\",\n",
159
+ " \"updated_at\": \"2023-02-24T04:08:16.142-05:00\",\n",
160
+ " \"md5\": \"21fa0fed0c5acd15cbf4f44018466649\",\n",
161
+ " \"file_ext\": \"jpg\",\n",
162
+ " \"file_size\": 363768,\n",
163
+ " \"image_width\": 621,\n",
164
+ " \"image_height\": 1839,\n",
165
+ " \"duration\": null,\n",
166
+ " \"status\": \"active\",\n",
167
+ " \"file_key\": \"eVEneY13S\",\n",
168
+ " \"is_public\": true,\n",
169
+ " \"pixel_hash\": \"190ffd3b86ce8332883b88b6af8f3572\",\n",
170
+ " \"variants\": [\n",
171
+ " {\n",
172
+ " \"type\": \"180x180\",\n",
173
+ " \"url\": \"https://cdn.donmai.us/180x180/21/fa/21fa0fed0c5acd15cbf4f44018466649.jpg\",\n",
174
+ " \"width\": 61,\n",
175
+ " \"height\": 180,\n",
176
+ " \"file_ext\": \"jpg\"\n",
177
+ " },\n",
178
+ " {\n",
179
+ " \"type\": \"360x360\",\n",
180
+ " \"url\": \"https://cdn.donmai.us/360x360/21/fa/21fa0fed0c5acd15cbf4f44018466649.jpg\",\n",
181
+ " \"width\": 122,\n",
182
+ " \"height\": 360,\n",
183
+ " \"file_ext\": \"jpg\"\n",
184
+ " },\n",
185
+ " {\n",
186
+ " \"type\": \"720x720\",\n",
187
+ " \"url\": \"https://cdn.donmai.us/720x720/21/fa/21fa0fed0c5acd15cbf4f44018466649.webp\",\n",
188
+ " \"width\": 243,\n",
189
+ " \"height\": 720,\n",
190
+ " \"file_ext\": \"webp\"\n",
191
+ " },\n",
192
+ " {\n",
193
+ " \"type\": \"original\",\n",
194
+ " \"url\": \"https://cdn.donmai.us/original/21/fa/21fa0fed0c5acd15cbf4f44018466649.jpg\",\n",
195
+ " \"width\": 621,\n",
196
+ " \"height\": 1839,\n",
197
+ " \"file_ext\": \"jpg\"\n",
198
+ " }\n",
199
+ " ]\n",
200
+ " },\n",
201
+ " \"tag_string_general\": \"4koma 6+girls comic danmaku multiple_girls\",\n",
202
+ " \"tag_string_character\": \"komeiji_satori kurodani_yamame mizuhashi_parsee reiuji_utsuho saigyouji_yuyuko yasaka_kanako\",\n",
203
+ " \"tag_string_copyright\": \"touhou touhou_tag_dream\",\n",
204
+ " \"tag_string_artist\": \"dei_shirou\",\n",
205
+ " \"tag_string_meta\": \"check_translation commentary_request highres translated translation_request\",\n",
206
+ " \"file_url\": \"https://cdn.donmai.us/original/21/fa/21fa0fed0c5acd15cbf4f44018466649.jpg\",\n",
207
+ " \"large_file_url\": \"https://cdn.donmai.us/original/21/fa/21fa0fed0c5acd15cbf4f44018466649.jpg\",\n",
208
+ " \"preview_file_url\": \"https://cdn.donmai.us/180x180/21/fa/21fa0fed0c5acd15cbf4f44018466649.jpg\"\n",
209
+ "}\n"
210
+ ]
211
+ }
212
+ ],
213
+ "metadata": {
214
+ "kernelspec": {
215
+ "display_name": "Python 3 (ipykernel)",
216
+ "language": "python",
217
+ "name": "python3"
218
+ },
219
+ "language_info": {
220
+ "codemirror_mode": {
221
+ "name": "ipython",
222
+ "version": 3
223
+ },
224
+ "file_extension": ".py",
225
+ "mimetype": "text/x-python",
226
+ "name": "python",
227
+ "nbconvert_exporter": "python",
228
+ "pygments_lexer": "ipython3",
229
+ "version": "3.8.10"
230
+ }
231
+ },
232
+ "nbformat": 4,
233
+ "nbformat_minor": 5
234
+ }