如何在文本文件中搜索关键字组合,提取上下行,然后使用 pandas 导出到 Excel
How to search for a combination of keywords in a text-file, extract lines above and below, and then export to Excel using pandas
我正在尝试从几个 SEC 10-K 文件中提取特定关键字组合前后的 5 行,然后将该数据导出到 Excel,以便我可以进一步手动处理它。
不幸的是,我不得不依赖 .txt 格式的文件,而不是 .html 或 .xblr 格式的文件,因为后者并不总是可用。我已经下载并部分清理了 .txt 文件以删除不需要的标签。
简而言之,我的目标是告诉 python 遍历下载的 .txt 文件(例如,同一文件夹中的所有文件,或者仅通过提供包含所有文件名的参考 .txt 列表),打开每一个,寻找单词 "cumulative effect"(最好与其他关键字结合使用,请参见下面的代码),提取其前后 5 行,然后将输出导出到文件名为 excel 的 excel A 列和 B 列中提取的段落。
使用 I managed to extract 5 lines above and below the keyword "cumulative effect" for one .txt file (which you can find here,供参考)。
然而,我仍在努力处理 automating/looping 整个过程,并使用 pandas 将提取的文本导出到 Excel。
import collections
import itertools
import sys
from pandas import DataFrame
filing='0000950123-94-002010_1.txt'
#with open(filing, 'r') as f:
with open(filing, 'r', encoding='utf-8', errors='replace') as f:
before = collections.deque(maxlen=5)
for line in f:
if ('cumulative effect' in line or 'Cumulative effect' in line) and ('accounting change' in line or 'adoption' in line or 'adopted' in line or 'charge' in line):
sys.stdout.writelines(before)
sys.stdout.write(line)
sys.stdout.writelines(itertools.islice(f, 5))
break
before.append(line)
findings = {'Filing': [filing],
'Extracted_paragraph': [line]
}
df = DataFrame(findings, columns= ['Filing', 'Extracted_paragraph'])
export_excel = df.to_excel (r'/Users/myname/PYTHON/output.xlsx', index = None, header=True)
print (df)
使用这行代码我获得了我需要的段落,但我只设法将包含关键字的单行导出到 excel 而不是整个文本。
This is the python output 和
this is the exported text to Excel.
如何创建循环并将整个感兴趣的段落正确导出到 excel?
提前致谢!!
我认为您的基本错误在于
'Extracted_paragraph': [line]
应该是
'Extracted_paragraph': [before]
因此,通过一些简化的更改,代码的主要部分应如下所示:
with open(filing, 'r', encoding='utf-8', errors='replace') as f:
before = collections.deque(maxlen=5)
for line in f:
if ('cumulative effect' in line or 'Cumulative effect' in line) and ('accounting change' in line or 'adoption' in line or 'adopted' in line or 'charge' in line):
break
before.append(line)
before = ''.join(before)
findings = {'Filing': [filing],
'Extracted_paragraph': [before]
}
df = DataFrame(findings, columns= ['Filing', 'Extracted_paragraph'])
然后从那里继续导出到Excel,等等
我正在尝试从几个 SEC 10-K 文件中提取特定关键字组合前后的 5 行,然后将该数据导出到 Excel,以便我可以进一步手动处理它。 不幸的是,我不得不依赖 .txt 格式的文件,而不是 .html 或 .xblr 格式的文件,因为后者并不总是可用。我已经下载并部分清理了 .txt 文件以删除不需要的标签。
简而言之,我的目标是告诉 python 遍历下载的 .txt 文件(例如,同一文件夹中的所有文件,或者仅通过提供包含所有文件名的参考 .txt 列表),打开每一个,寻找单词 "cumulative effect"(最好与其他关键字结合使用,请参见下面的代码),提取其前后 5 行,然后将输出导出到文件名为 excel 的 excel A 列和 B 列中提取的段落。
使用
import collections
import itertools
import sys
from pandas import DataFrame
filing='0000950123-94-002010_1.txt'
#with open(filing, 'r') as f:
with open(filing, 'r', encoding='utf-8', errors='replace') as f:
before = collections.deque(maxlen=5)
for line in f:
if ('cumulative effect' in line or 'Cumulative effect' in line) and ('accounting change' in line or 'adoption' in line or 'adopted' in line or 'charge' in line):
sys.stdout.writelines(before)
sys.stdout.write(line)
sys.stdout.writelines(itertools.islice(f, 5))
break
before.append(line)
findings = {'Filing': [filing],
'Extracted_paragraph': [line]
}
df = DataFrame(findings, columns= ['Filing', 'Extracted_paragraph'])
export_excel = df.to_excel (r'/Users/myname/PYTHON/output.xlsx', index = None, header=True)
print (df)
使用这行代码我获得了我需要的段落,但我只设法将包含关键字的单行导出到 excel 而不是整个文本。 This is the python output 和 this is the exported text to Excel.
如何创建循环并将整个感兴趣的段落正确导出到 excel? 提前致谢!!
我认为您的基本错误在于
'Extracted_paragraph': [line]
应该是
'Extracted_paragraph': [before]
因此,通过一些简化的更改,代码的主要部分应如下所示:
with open(filing, 'r', encoding='utf-8', errors='replace') as f:
before = collections.deque(maxlen=5)
for line in f:
if ('cumulative effect' in line or 'Cumulative effect' in line) and ('accounting change' in line or 'adoption' in line or 'adopted' in line or 'charge' in line):
break
before.append(line)
before = ''.join(before)
findings = {'Filing': [filing],
'Extracted_paragraph': [before]
}
df = DataFrame(findings, columns= ['Filing', 'Extracted_paragraph'])
然后从那里继续导出到Excel,等等