File size: 4,688 Bytes
5597066 |
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 |
{
"cells": [
{
"cell_type": "markdown",
"id": "dc4f5934-9bb9-4bdc-975a-86bed379f9b5",
"metadata": {},
"source": [
"好的,我来修改脚本以满足您的要求:\n",
"\n",
"```python\n",
"import pandas as pd\n",
"import os\n",
"\n",
"# 读取parquet文件\n",
"df = pd.read_parquet(\"exp2/table.parquet\")\n",
"\n",
"# 创建输出目录(如果不存在)\n",
"output_dir = \"output_txt\"\n",
"if not os.path.exists(output_dir):\n",
" os.makedirs(output_dir)\n",
"\n",
"# 遍历每一行数据\n",
"for _, row in df.iterrows():\n",
" # 处理每个字段,将空格分隔的标签转换为逗号加空格分隔\n",
" artist = row['tag_string_artist'].replace(' ', ', ')\n",
" character = row['tag_string_character'].replace(' ', ', ')\n",
" general = row['tag_string_general'].replace(' ', ', ')\n",
" meta = row['tag_string_meta'].replace(' ', ', ')\n",
" \n",
" # 组合所有标签,使用逗号和空格分隔\n",
" content = f\"{artist}, {character}, {general}, {meta}\"\n",
" \n",
" # 使用id作为文件名\n",
" filename = os.path.join(output_dir, f\"{row['id']}.txt\")\n",
" \n",
" # 写入文件\n",
" with open(filename, 'w', encoding='utf-8') as f:\n",
" f.write(content)\n",
"\n",
"print(\"处理完成!文件已保存到output_txt目录下\")\n",
"print(f\"共处理了 {len(df)} 个文件\")\n",
"```\n",
"\n",
"这样生成的txt文件内容会像这样:\n",
"```\n",
"nznzn, jinhsi_(wuthering_waves), 1girl, solo, absurdres, highres\n",
"```\n",
"\n",
"所有标签都会按照 artist, character, general, meta 的顺序排列,并用逗号加空格分隔。需要我运行这个脚本吗?"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fe7391e4-f254-4003-80ef-3b633ae7c092",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import os\n",
"from datetime import datetime\n",
"\n",
"# 读取parquet文件\n",
"df = pd.read_parquet(\"./table.parquet\")\n",
"\n",
"# 创建输出目录(如果不存在)\n",
"output_dir = \"output_txt\"\n",
"if not os.path.exists(output_dir):\n",
" os.makedirs(output_dir)\n",
"\n",
"# 遍历每一行数据\n",
"for _, row in df.iterrows():\n",
" # 处理每个字段,将空格分隔的标签转换为逗号加空格分隔\n",
" artist = str(row['tag_string_artist']).replace(' ', ', ') if row['tag_string_artist'] else ''\n",
" character = str(row['tag_string_character']).replace(' ', ', ') if row['tag_string_character'] else ''\n",
" general = str(row['tag_string_general']).replace(' ', ', ') if row['tag_string_general'] else ''\n",
" meta = str(row['tag_string_meta']).replace(' ', ', ') if row['tag_string_meta'] else ''\n",
" copyright = str(row['tag_string_copyright']).replace(' ', ', ') if row['tag_string_copyright'] else ''\n",
" \n",
" # 根据created_at添加年份标签\n",
" created_at = datetime.fromisoformat(row['created_at'])\n",
" if created_at.year == 2024:\n",
" year_tag = \"year_2024\"\n",
" elif created_at.year == 2023:\n",
" year_tag = \"year_2023\"\n",
" elif created_at.year == 2022:\n",
" year_tag = \"year_2022\"\n",
" elif created_at.year == 2021:\n",
" year_tag = \"year_2021\"\n",
" else:\n",
" year_tag = f\"year_{created_at.year}\"\n",
" \n",
" # 组合所有标签,使用逗号和空格分隔\n",
" # 可以修改顺序\n",
" content = f\"{artist}, {character}, {year_tag}, {general}, {meta}, {copyright}\"\n",
" \n",
" # 使用id作为文件名\n",
" filename = os.path.join(output_dir, f\"{row['id']}.txt\")\n",
" \n",
" # 写入文件\n",
" with open(filename, 'w', encoding='utf-8') as f:\n",
" f.write(content)\n",
"\n",
"print(\"处理完成!文件已保存到output_txt目录下\")\n",
"print(f\"共处理了 {len(df)} 个文件\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.10.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|