import re
import pandas as pd
from tqdm import tqdm
def get_re_data():
df_re = pd.read_excel('all_regex.xlsx',sheet_name=0)
return df_re
def get_passage_data():
df_passage = pd.read_excel('news001.xlsx',sheet_name=0)
return df_passage
df_re = get_re_data()
df_passage = get_passage_data()
遍历文章,验证正则
num = 0
re_result_list = []
passage_result_list = []
for one_re in df_re['regex']:
r1 = re.compile(one_re, re.S)
# print(one_re)
for one_passage in tqdm(df_passage['cont']):
one_list = r1.findall(str(one_passage))
if one_list != []:
# print('单篇匹配到的结果数是')
# print(len(one_list))
num += 1
# print(one_re)
# print(one_list)
passage_result_list.append(one_passage)
re_result_list.append(one_re)
print('总文章结果list长度是')
print(len(passage_result_list))
print('总正则结果list长度是')
print(len(re_result_list))
print('能匹配到文章的正则:{}条'.format(len(set(re_result_list))))
print('匹配上的文章数:{}篇'.format(len(set(passage_result_list))))
df_final_result = pd.DataFrame({'re':re_result_list,'passage':passage_result_list})
print(df_final_result)
df_final_result.to_csv('match_result.csv',encoding='utf_8_sig')