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