Skip to main content
 首页 » 编程设计

python之使用 python 从文章中提取数据的多个正则表达式模式

2024年12月31日30xing901022

Python 的新手,但对生活来说却很老。 我试图使用 txt 文件中的多个正则表达式模式从新闻文章 txt 文件中提取数据。我已经到了可以找到匹配但不保存提取的数据的地步。到目前为止,这是我在原始的不卫生的非 pythonic 脚本中所拥有的。感谢所有评论,因为我正在自学。

import re 
 
reg_ex = open('APT1.txt', "r", encoding = 'utf-8-sig') 
lines = reg_ex.read() 
strip = lines.strip() 
reggie = strip.split(';')  
 
 
reggie_lst = [] 
match_lst = [] 
 
for raw_regex in reggie: 
    reggie_lst.append(re.compile(raw_regex)) 
 
 
get_string = open("APT.txt", "r", encoding = 'utf-8-sig') 
nystring = get_string.read() 
 
 
if any(compiled_reg.search(nystring) for compiled_reg in reggie_lst): 
    print("Got some Matches") 

请您参考如下方法:

您可以使用 re.findall() 将您的数据提取到列表中,而不是仅仅询问正则表达式是否匹配。

import re 
 
reg_ex = open('APT1.txt', "r", encoding='utf-8-sig') 
lines = reg_ex.read() 
strip = lines.strip() 
reggie = strip.split(';') 
 
reggie_lst = [] 
match_lst = [] 
 
for raw_regex in reggie: 
    reggie_lst.append(raw_regex) 
 
get_string = open("APT.txt", "r", encoding='utf-8-sig') 
nystring = get_string.read() 
 
 
for reg in reggie_lst: 
    for text_match in re.findall(reg, nystring): 
        print("Got match for regex {}: {}".format(reg, text_match)) 

当然,除了在最后一行打印它,您还可以将它保存在一个新文件中。在此示例中,我还删除了仅为打印/调试目的编译正则表达式。

在正则表达式中使用括号(组)时要小心。 re.findall() 行为与 re.search() 或 re.match() 略有不同。您必须使用 (?: … 然后,另请参阅 this post 。