Python 具有循环结果的文本文件生成
我有一个包含32篇文章的文本文件。我设法找到包含以下代码的每篇文章:Python 具有循环结果的文本文件生成,python,Python,我有一个包含32篇文章的文本文件。我设法找到包含以下代码的每篇文章: import re sections = [] current = [] with open("Aberdeen2005.txt") as f: for line in f: if re.search(r"(?i)\d+ of \d+ DOCUMENTS", line): sections.append("".join(current))
import re
sections = []
current = []
with open("Aberdeen2005.txt") as f:
for line in f:
if re.search(r"(?i)\d+ of \d+ DOCUMENTS", line):
sections.append("".join(current))
current = [line]
else:
current.append(line)
print(len(sections))
接下来我做的事情是看看有多少文章有我感兴趣的关键词:税收和政策。在这行中,如果文章中有,我将提取月份:
months=['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'Novemeber', 'December']
for i in range(len(sections)):
if (' tax ' in sections[i]
or ' Tax ' in sections[i]
or ' policy ' in sections[i]
or ' Policy ' in sections[i]):
pat=re.compile("|".join([r"\b{}\b".format(m) for m in months]), re.M)
month = pat.search("\n".join(sections[i].splitlines()[0:6]))
print(month)
最后但并非最不重要的一点是,我想创建一个包含以前发现的月份的文本文件:
outfile = open('C:/Users/nn/Desktop/Uncertainty_Scot/dates.txt', 'w')
outfile.write(month.group(0))
outfile.close
问题就在这里,它只生产最后一个月的产品。我猜是因为它不在循环中,有什么办法吗
亲切的问候 对于输出文件,只需将循环包装在
中,并使用
循环,如下所示:
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
with open(r'C:\Users\nn\Desktop\Uncertainty_Scot\dates.txt', 'w') as outfile:
for i in range(len(sections)):
if (' tax ' in sections[i] or ' Tax ' in sections[i] or ' policy ' in sections[i] or ' Policy ' in sections[i]):
pat = re.compile("|".join([r"\b{}\b".format(m) for m in months]), re.M)
month = pat.search("\n".join(sections[i].splitlines()[0:6]))
print(month)
outfile.write(month.group(0))
您可以通过执行以下操作进一步改进循环:
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
with open('C:/Users/nn/Desktop/Uncertainty_Scot/dates.txt', 'w') as outfile:
for s in sections:
if any(x in s.lower() for x in [' tax ', ' policy ']:
pat = re.compile("|".join([r"\b{}\b".format(m) for m in months]), re.M)
month = pat.search("\n".join(s.splitlines()[0:6]))
print(month)
outfile.write(month.group(0))
通过首先转换为小写,您只需测试字符串的一个版本,它还将捕获形式为
“TAX”
的条目非常感谢您的回答和澄清,它看起来很棒。当我运行代码时,我得到以下信息:“NoneType”对象没有属性“group”。知道这个错误是从哪里来的吗?Kind regardsIt表示您的一行与正则表达式不匹配。您可以在打印和编写之前添加if month:
。