You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
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文件