From 7c181721a6bafe231f1cd82e5e4732f7c7644005 Mon Sep 17 00:00:00 2001 From: frxz751113 <156018267+frxz751113@users.noreply.github.com> Date: Mon, 26 Aug 2024 00:40:28 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E4=B8=80=E9=94=85=E7=83=A9.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- py/一锅烩.py | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/py/一锅烩.py b/py/一锅烩.py index 72fd5e0..91d70ba 100644 --- a/py/一锅烩.py +++ b/py/一锅烩.py @@ -221,7 +221,7 @@ replacements = { with open('汇总.txt', 'r', encoding='utf-8') as file: lines = file.readlines() # 创建新文件并写入替换后的内容 -with open('汇总.txt', 'w', encoding='utf-8') as new_file: +with open('2.txt', 'w', encoding='utf-8') as new_file: for line in lines: for old, new in replacements.items(): line = line.replace(old, new) @@ -230,25 +230,6 @@ print("替换完成,新文件已保存。") - -import re -from pypinyin import lazy_pinyin -# 打开一个utf-8编码的文本文件 -with open("汇总.txt", "r", encoding="utf-8") as file: - # 读取所有行并存储到列表中 - lines = file.readlines() -# 定义一个函数,用于提取每行的第一个数字 -def extract_first_number(line): - match = re.search(r'\d+', line) - return int(match.group()) if match else float('inf') -# 对列表中的行进行排序,按照第一个数字的大小排列,其余行按中文排序 -sorted_lines = sorted(lines, key=lambda x: (not 'CCTV' in x, extract_first_number(x) if 'CCTV' in x else lazy_pinyin(x.strip()))) -# 将排序后的行写入新的utf-8编码的文本文件 -with open("2.txt", "w", encoding="utf-8") as file: - for line in sorted_lines: - file.write(line) - - import re import os def filter_lines(file_path): @@ -362,6 +343,22 @@ with open('2.txt', 'w', encoding='utf-8') as new_file: new_file.write(line) # 写入新文件 print("新文件已保存。") # 打印完成信息 +import re +from pypinyin import lazy_pinyin +# 打开一个utf-8编码的文本文件 +with open("2.txt", "r", encoding="utf-8") as file: + # 读取所有行并存储到列表中 + lines = file.readlines() +# 定义一个函数,用于提取每行的第一个数字 +def extract_first_number(line): + match = re.search(r'\d+', line) + return int(match.group()) if match else float('inf') +# 对列表中的行进行排序,按照第一个数字的大小排列,其余行按中文排序 +sorted_lines = sorted(lines, key=lambda x: (not 'CCTV' in x, extract_first_number(x) if 'CCTV' in x else lazy_pinyin(x.strip()))) +# 将排序后的行写入新的utf-8编码的文本文件 +with open("2.txt", "w", encoding="utf-8") as file: + for line in sorted_lines: + file.write(line) ########################################################################################################################################################################################