ACCC1380 commited on
Commit
5db5d7f
1 Parent(s): d72567e

Upload json2txt_new (4).ipynb

Browse files
Files changed (1) hide show
  1. 3w/json2txt_new (4).ipynb +149 -0
3w/json2txt_new (4).ipynb ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": null,
6
+ "id": "590669bd-a7eb-46b8-b202-fd4a611bef08",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "import os\n",
11
+ "import json\n",
12
+ "from datetime import datetime\n",
13
+ "from urllib.parse import urlparse\n",
14
+ "\n",
15
+ "# 遍历所有目录下的 JSON 文件\n",
16
+ "def traverse_and_process(root_dir):\n",
17
+ " for root, dirs, files in os.walk(root_dir):\n",
18
+ " for file in files:\n",
19
+ " if file.endswith('.json'):\n",
20
+ " input_file = os.path.join(root, file)\n",
21
+ " process_file(input_file)\n",
22
+ "\n",
23
+ "# 处理单个文件\n",
24
+ "def process_file(input_file):\n",
25
+ " count = 1\n",
26
+ " with open(input_file, 'r', encoding='utf-8') as file:\n",
27
+ " for line in file:\n",
28
+ " process_line(line, count, input_file)\n",
29
+ " count += 1\n",
30
+ "\n",
31
+ "def process_line(line, count, input_file):\n",
32
+ " try:\n",
33
+ " data = json.loads(line)\n",
34
+ " # 提取关键数据转为txt\n",
35
+ " json2txt(data, input_file)\n",
36
+ " print(f'Processed line {count}')\n",
37
+ " except json.JSONDecodeError as e:\n",
38
+ " print(f\"JSONDecodeError: {e}\")\n",
39
+ " except KeyError as e:\n",
40
+ " print(f\"KeyError: Missing key {e}\")\n",
41
+ "\n",
42
+ "def json2txt(data, input_file):\n",
43
+ " try:\n",
44
+ " danbooru_data = data['danbooru']\n",
45
+ " \n",
46
+ " # 提取关键参数\n",
47
+ " id_value = str(danbooru_data['id'])\n",
48
+ " tag_general = danbooru_data.get('tag_string_general', '')\n",
49
+ " tag_character = danbooru_data.get('tag_string_character', '')\n",
50
+ " tag_copyright = danbooru_data.get('tag_string_copyright', '')\n",
51
+ " tag_artist = danbooru_data.get('tag_string_artist', '')\n",
52
+ " tag_meta = danbooru_data.get('tag_string_meta', '')\n",
53
+ " rating = danbooru_data['rating']\n",
54
+ " created_at = danbooru_data['created_at']\n",
55
+ " file_url = danbooru_data['file_url']\n",
56
+ " \n",
57
+ " # 格式化标签字符串\n",
58
+ " def format_tags(tag_string):\n",
59
+ " tags = tag_string.split()\n",
60
+ " formatted_tags = ', '.join(tags)\n",
61
+ " return formatted_tags\n",
62
+ "\n",
63
+ " tag_general_formatted = format_tags(tag_general)\n",
64
+ " tag_character_formatted = format_tags(tag_character)\n",
65
+ " tag_copyright_formatted = format_tags(tag_copyright)\n",
66
+ " tag_artist_formatted = format_tags(tag_artist)\n",
67
+ " tag_meta_formatted = format_tags(tag_meta)\n",
68
+ " \n",
69
+ " # 构造文件名\n",
70
+ " filename = f\"danbooru_{id_value}.txt\"\n",
71
+ " year = datetime.strptime(created_at, \"%Y-%m-%dT%H:%M:%S.%f%z\").year\n",
72
+ " \n",
73
+ " # 获取相对文件夹路径\n",
74
+ " relative_directory = os.path.dirname(input_file)\n",
75
+ " if not os.path.exists(relative_directory):\n",
76
+ " os.makedirs(relative_directory)\n",
77
+ " \n",
78
+ " # 写入文本文件\n",
79
+ " file_path = os.path.join(relative_directory, filename)\n",
80
+ " with open(file_path, 'w', encoding='utf-8') as f:\n",
81
+ " f.write(f\"{tag_artist_formatted}, \")\n",
82
+ " f.write(f\"{tag_meta_formatted}, \")\n",
83
+ " f.write(f\"{tag_general_formatted}, \")\n",
84
+ " f.write(f\"{tag_character_formatted}, \")\n",
85
+ " f.write(f\"{tag_copyright_formatted}, \")\n",
86
+ " f.write(f\"year_{year}, \")\n",
87
+ " if rating == 'e':\n",
88
+ " f.write(\"nsfw\")\n",
89
+ " print(f\"文件 {file_path} 已成功创建并保存相关数据。\")\n",
90
+ " \n",
91
+ " except KeyError as e:\n",
92
+ " print(f\"KeyError: Missing key {e}\")\n",
93
+ "\n",
94
+ "# 设置根目录路径\n",
95
+ "root_dir = 'data2'\n",
96
+ "\n",
97
+ "# 开始遍历和处理\n",
98
+ "traverse_and_process(root_dir)\n"
99
+ ]
100
+ },
101
+ {
102
+ "cell_type": "code",
103
+ "execution_count": null,
104
+ "id": "b2ff12cb-b06f-4f4d-82fb-1ed6702d606b",
105
+ "metadata": {},
106
+ "outputs": [],
107
+ "source": [
108
+ "import os\n",
109
+ "\n",
110
+ "def delete_txt_files(directory):\n",
111
+ " for root, dirs, files in os.walk(directory):\n",
112
+ " for file in files:\n",
113
+ " if file.endswith(\".json\"):\n",
114
+ " file_path = os.path.join(root, file)\n",
115
+ " try:\n",
116
+ " os.remove(file_path)\n",
117
+ " print(f\"Deleted: {file_path}\")\n",
118
+ " except Exception as e:\n",
119
+ " print(f\"Failed to delete {file_path}. Reason: {e}\")\n",
120
+ "\n",
121
+ "# Specify the directory to start from\n",
122
+ "start_directory = './'\n",
123
+ "\n",
124
+ "delete_txt_files(start_directory)\n"
125
+ ]
126
+ }
127
+ ],
128
+ "metadata": {
129
+ "kernelspec": {
130
+ "display_name": "Python 3",
131
+ "language": "python",
132
+ "name": "python3"
133
+ },
134
+ "language_info": {
135
+ "codemirror_mode": {
136
+ "name": "ipython",
137
+ "version": 3
138
+ },
139
+ "file_extension": ".py",
140
+ "mimetype": "text/x-python",
141
+ "name": "python",
142
+ "nbconvert_exporter": "python",
143
+ "pygments_lexer": "ipython3",
144
+ "version": "3.10.10"
145
+ }
146
+ },
147
+ "nbformat": 4,
148
+ "nbformat_minor": 5
149
+ }