import re # 指定要搜索的文件名 filename = "总弹幕.txt" # 定义要搜索的关键字 keywords = ["AI","ai","8K","8k","自动生成","定制化","3D","3d","全息视频","视障人士"] # 正则表达式模式,用于匹配包含所有关键字的行 pattern = r'(?={})'.format('|'.join(keywords)) # 创建一个空列表来存放包含所有关键字的行 relevant_lines = [] # 打开文件并逐行读取 with open(filename, 'r',encoding='utf-8') as file: for line_number, line in enumerate(file, 1): # 如果行中包含所有关键字,将其添加到relevant_lines列表中 if re.search(pattern, line, re.IGNORECASE): relevant_lines.append(f"{line.strip()}") # 打印结果 with open('AI弹幕.txt',mode='w',encoding='utf-8')as f: for item in relevant_lines: f.write(f"{item}\n")#写入text文件