From df562a23b83e67988a372197ebb5a63968dd41ea Mon Sep 17 00:00:00 2001 From: frxz751113 <156018267+frxz751113@users.noreply.github.com> Date: Mon, 26 Aug 2024 19:06:10 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E6=94=B6=E9=9B=86.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- py/收集.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/py/收集.py b/py/收集.py index d79fd7c..328d0e1 100644 --- a/py/收集.py +++ b/py/收集.py @@ -428,4 +428,31 @@ for file in files_to_remove: else: # 如果文件不存在,则提示异常并打印提示信息 print(f"文件 {file} 不存在,跳过删除。") -print("任务运行完毕,分类频道列表可查看文件夹内综合源.txt文件!") +# 打开文本文件进行读取 +with open('网络收集.txt', 'r', encoding='utf-8') as file: + lines = file.readlines() +# 初始化一个计数器来跟踪遇到的第一个包含"genre"的行 +first_genre_encountered = False +# 用于存储处理后的行的列表 +processed_lines = [] +# 处理每一行 +for line in lines: + if 'genre' in line: + if not first_genre_encountered: + # 如果这是第一个包含"genre"的行,标记已遇到并继续读取 + first_genre_encountered = True + else: + # 如果已经遇到第一个包含"genre"的行,跳过这个包含"genre"的行 + continue + else: + # 对于不包含"genre"的行,总是写入 + processed_lines.append(line) +# 特殊情况:如果文件中只有一个包含"genre"的行,那么它应该被写入 +if not first_genre_encountered: + for line in lines: + if 'genre' in line: + processed_lines.append(line) +# 将处理后的行写入到输出文件 +with open('网络收集.txt', 'w', encoding='utf-8') as outfile: + outfile.writelines(processed_lines) +print("任务运行完毕,频道列表可查看文件夹内源.txt文件!")