任务描述
附件中为工作报告,本题要求将报告中所有包含数字字符的短句(*)进行提取,并根据输入的关键字信息,将报告中关联数据筛选后输出(限于篇幅,本题仅列出部分关联词)。
附件在哪里
不知道你这个问题是否已经解决, 如果还没有解决的话:if now > times:
while True:
try:
if browser.find_element_by_id("J_SelectAll2"):
browser.find_element_by_id("J_SelectAll2").click()
break
except:
print("找不到购买按钮")
# 点击结算按钮
while True:
try:
if browser.find_element_by_link_text("结 算"):
browser.find_element_by_link_text("结 算").click()
print("结算成功")
break
except:
pass
我的解决方案如下:
1.读取工作报告的文本内容,并将其保存在一个字符串变量中。
with open('工作报告.txt', 'r', encoding='utf-8') as f:
content = f.read()
2.使用Python的正则表达式来匹配文本中的数字字符短语。我们可以使用re.findall()
方法来查找内容中的所有数字字符短语。
import re
pattern = '\d+[\.]?\d*'
matches = re.findall(pattern, content)
keywords = ['关键字1', '关键字2', '关键字3']
matched_phrases = []
for match in matches:
for keyword in keywords:
if keyword in match:
matched_phrases.append(match)
break
print()
函数来输出得到的结果。print(matched_phrases)
完整代码如下:
import re
with open('工作报告.txt', 'r', encoding='utf-8') as f:
content = f.read()
pattern = '\d+[\.]?\d*'
matches = re.findall(pattern, content)
keywords = ['关键字1', '关键字2', '关键字3']
matched_phrases = []
for match in matches:
for keyword in keywords:
if keyword in match:
matched_phrases.append(match)
break
print(matched_phrases)