Upload exp2/Untitled2.ipynb with huggingface_hub
Browse files- exp2/Untitled2.ipynb +130 -0
exp2/Untitled2.ipynb
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"id": "dc4f5934-9bb9-4bdc-975a-86bed379f9b5",
|
6 |
+
"metadata": {},
|
7 |
+
"source": [
|
8 |
+
"好的,我来修改脚本以满足您的要求:\n",
|
9 |
+
"\n",
|
10 |
+
"```python\n",
|
11 |
+
"import pandas as pd\n",
|
12 |
+
"import os\n",
|
13 |
+
"\n",
|
14 |
+
"# 读取parquet文件\n",
|
15 |
+
"df = pd.read_parquet(\"exp2/table.parquet\")\n",
|
16 |
+
"\n",
|
17 |
+
"# 创建输出目录(如果不存在)\n",
|
18 |
+
"output_dir = \"output_txt\"\n",
|
19 |
+
"if not os.path.exists(output_dir):\n",
|
20 |
+
" os.makedirs(output_dir)\n",
|
21 |
+
"\n",
|
22 |
+
"# 遍历每一行数据\n",
|
23 |
+
"for _, row in df.iterrows():\n",
|
24 |
+
" # 处理每个字段,将空格分隔的标签转换为逗号加空格分隔\n",
|
25 |
+
" artist = row['tag_string_artist'].replace(' ', ', ')\n",
|
26 |
+
" character = row['tag_string_character'].replace(' ', ', ')\n",
|
27 |
+
" general = row['tag_string_general'].replace(' ', ', ')\n",
|
28 |
+
" meta = row['tag_string_meta'].replace(' ', ', ')\n",
|
29 |
+
" \n",
|
30 |
+
" # 组合所有标签,使用逗号和空格分隔\n",
|
31 |
+
" content = f\"{artist}, {character}, {general}, {meta}\"\n",
|
32 |
+
" \n",
|
33 |
+
" # 使用id作为文件名\n",
|
34 |
+
" filename = os.path.join(output_dir, f\"{row['id']}.txt\")\n",
|
35 |
+
" \n",
|
36 |
+
" # 写入文件\n",
|
37 |
+
" with open(filename, 'w', encoding='utf-8') as f:\n",
|
38 |
+
" f.write(content)\n",
|
39 |
+
"\n",
|
40 |
+
"print(\"处理完成!文件已保存到output_txt目录下\")\n",
|
41 |
+
"print(f\"共处理了 {len(df)} 个文件\")\n",
|
42 |
+
"```\n",
|
43 |
+
"\n",
|
44 |
+
"这样生成的txt文件内容会像这样:\n",
|
45 |
+
"```\n",
|
46 |
+
"nznzn, jinhsi_(wuthering_waves), 1girl, solo, absurdres, highres\n",
|
47 |
+
"```\n",
|
48 |
+
"\n",
|
49 |
+
"所有标签都会按照 artist, character, general, meta 的顺序排列,并用逗号加空格分隔。需要我运行这个脚本吗?"
|
50 |
+
]
|
51 |
+
},
|
52 |
+
{
|
53 |
+
"cell_type": "code",
|
54 |
+
"execution_count": null,
|
55 |
+
"id": "fe7391e4-f254-4003-80ef-3b633ae7c092",
|
56 |
+
"metadata": {},
|
57 |
+
"outputs": [],
|
58 |
+
"source": [
|
59 |
+
"import pandas as pd\n",
|
60 |
+
"import os\n",
|
61 |
+
"from datetime import datetime\n",
|
62 |
+
"\n",
|
63 |
+
"# 读取parquet文件\n",
|
64 |
+
"df = pd.read_parquet(\"./table.parquet\")\n",
|
65 |
+
"\n",
|
66 |
+
"# 创建输出目录(如果不存在)\n",
|
67 |
+
"output_dir = \"output_txt\"\n",
|
68 |
+
"if not os.path.exists(output_dir):\n",
|
69 |
+
" os.makedirs(output_dir)\n",
|
70 |
+
"\n",
|
71 |
+
"# 遍历每一行数据\n",
|
72 |
+
"for _, row in df.iterrows():\n",
|
73 |
+
" # 处理每个字段,将空格分隔的标签转换为逗号加空格分隔\n",
|
74 |
+
" artist = str(row['tag_string_artist']).replace(' ', ', ') if row['tag_string_artist'] else ''\n",
|
75 |
+
" character = str(row['tag_string_character']).replace(' ', ', ') if row['tag_string_character'] else ''\n",
|
76 |
+
" general = str(row['tag_string_general']).replace(' ', ', ') if row['tag_string_general'] else ''\n",
|
77 |
+
" meta = str(row['tag_string_meta']).replace(' ', ', ') if row['tag_string_meta'] else ''\n",
|
78 |
+
" copyright = str(row['tag_string_copyright']).replace(' ', ', ') if row['tag_string_copyright'] else ''\n",
|
79 |
+
" \n",
|
80 |
+
" # 根据created_at添加年份标签\n",
|
81 |
+
" created_at = datetime.fromisoformat(row['created_at'])\n",
|
82 |
+
" if created_at.year == 2024:\n",
|
83 |
+
" year_tag = \"year_2024\"\n",
|
84 |
+
" elif created_at.year == 2023:\n",
|
85 |
+
" year_tag = \"year_2023\"\n",
|
86 |
+
" elif created_at.year == 2022:\n",
|
87 |
+
" year_tag = \"year_2022\"\n",
|
88 |
+
" elif created_at.year == 2021:\n",
|
89 |
+
" year_tag = \"year_2021\"\n",
|
90 |
+
" else:\n",
|
91 |
+
" year_tag = f\"year_{created_at.year}\"\n",
|
92 |
+
" \n",
|
93 |
+
" # 组合所有标签,使用逗号和空格分隔\n",
|
94 |
+
" # 可以修改顺序\n",
|
95 |
+
" content = f\"{artist}, {character}, {year_tag}, {general}, {meta}, {copyright}\"\n",
|
96 |
+
" \n",
|
97 |
+
" # 使用id作为文件名\n",
|
98 |
+
" filename = os.path.join(output_dir, f\"{row['id']}.txt\")\n",
|
99 |
+
" \n",
|
100 |
+
" # 写入文件\n",
|
101 |
+
" with open(filename, 'w', encoding='utf-8') as f:\n",
|
102 |
+
" f.write(content)\n",
|
103 |
+
"\n",
|
104 |
+
"print(\"处理完成!文件已保存到output_txt目录下\")\n",
|
105 |
+
"print(f\"共处理了 {len(df)} 个文件\")"
|
106 |
+
]
|
107 |
+
}
|
108 |
+
],
|
109 |
+
"metadata": {
|
110 |
+
"kernelspec": {
|
111 |
+
"display_name": "Python 3",
|
112 |
+
"language": "python",
|
113 |
+
"name": "python3"
|
114 |
+
},
|
115 |
+
"language_info": {
|
116 |
+
"codemirror_mode": {
|
117 |
+
"name": "ipython",
|
118 |
+
"version": 3
|
119 |
+
},
|
120 |
+
"file_extension": ".py",
|
121 |
+
"mimetype": "text/x-python",
|
122 |
+
"name": "python",
|
123 |
+
"nbconvert_exporter": "python",
|
124 |
+
"pygments_lexer": "ipython3",
|
125 |
+
"version": "3.10.10"
|
126 |
+
}
|
127 |
+
},
|
128 |
+
"nbformat": 4,
|
129 |
+
"nbformat_minor": 5
|
130 |
+
}
|