From 1dd8fb4e71941ba7dd23923154c4a3e7a1265e58 Mon Sep 17 00:00:00 2001 From: frxz751113 <156018267+frxz751113@users.noreply.github.com> Date: Thu, 15 Aug 2024 15:41:03 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E6=BA=90=E9=AA=8C=E8=AF=81.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- py/源验证.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/py/源验证.py b/py/源验证.py index ee96dff..c179556 100644 --- a/py/源验证.py +++ b/py/源验证.py @@ -81,6 +81,39 @@ if __name__ == "__main__": main(source_file_path, output_file_path) # 调用main函数 except Exception as e: print(f"程序发生错误: {e}") # 打印错误信息 + +#########################################################################提取源1中的有效行 +def filter_lines(file_path): + with open(file_path, 'r', encoding='utf-8') as file: # 打开文件 + lines = file.readlines() # 读取所有行 + filtered_lines = [] # 初始化过滤后的行列表 + for line in lines: # 遍历所有行 + if 'genre' in line or '有效' in line: # 如果行中包含'genre'或'有效' + filtered_lines.append(line) # 将行添加到过滤后的行列表 + return filtered_lines # 返回过滤后的行列表 +def write_filtered_lines(output_file_path, filtered_lines): + with open(output_file_path, 'w', encoding='utf-8') as output_file: # 打开输出文件 + output_file.writelines(filtered_lines) # 写入过滤后的行 +if __name__ == "__main__": + input_file_path = "源1.txt" # 设置输入文件路径 + output_file_path = "源1.txt" # 设置输出文件路径 + filtered_lines = filter_lines(input_file_path) # 调用filter_lines函数 + write_filtered_lines(output_file_path, filtered_lines) # 调用write_filtered_lines函数 +###################################################################################定义替换规则的字典,对整行内的内容进行替换 +replacements = { + ",有效": "", # 将",有效"替换为空字符串 + "#genre#,无效": "#genre#", # 将"#genre#,无效"替换为"#genre#" +} +# 打开原始文件读取内容,并写入新文件 +with open('源1.txt', 'r', encoding='utf-8') as file: + lines = file.readlines() +# 创建新文件并写入替换后的内容 +with open('源1.txt', 'w', encoding='utf-8') as new_file: + for line in lines: + for old, new in replacements.items(): # 遍历替换规则字典 + line = line.replace(old, new) # 替换行中的内容 + new_file.write(line) # 写入新文件 +print("新文件已保存。") # 打印完成信息 # 初始化优选字典 detected_ips = {} # 存储文件路径