【发布时间】:2020-08-12 16:54:15
【问题描述】:
我已使用 pdfplumber 从 pdf 文件中提取文本。文本包含“Exhibit XY”格式的多个项目,其中 X 是字母,Y 是数字,例如展示 C40 或展示 R700。
我试图减少整个提取的文本,以简单地将各种 Exhibit XY 组合显示为列表。我最初的想法是将文本字符串转换为列表:
import pdfplumber
with pdfplumber.open(file) as pdf:
p1 = pdf.pages[0]
p2 = pdf.pages[1]
p3 = pdf.pages[2]
p1_text = p1.extract_text()
p2_text = p2.extract_text()
p3_text = p3.extract_text()
# print(p1_text, p2_text, p3_text)
full_text = p1_text + p2_text + p3_text
list_full_text = full_text.split()
pdfplumber的输出如下:
apple cars 2014 pizza hut. Aftermath, you tried an Exhibit R40; decidedly 50 times
larger than Exhibit C400. The 1,000 luckiest break had the under dome Exhibit R9.
Exhibit P21 as well. 0.1 you have not found it again. Exhibit CB12 district office see
Exhibit MM42.
在列表形式中,这是:
['apple', 'cars', '2014', 'pizza', 'hut.', 'Aftermath,', 'you', 'tried', 'an', 'Exhibit', 'R40;', 'decidedly', '50', 'times', 'larger', 'than', 'Exhibit', 'C400.', 'The', '1,000', 'luckiest', 'break', 'had', 'the', 'under', 'dome', 'Exhibit', 'R9.', 'Exhibit', 'P21', 'as', 'well.', '0.1', 'you', 'have', 'not', 'found', 'it', 'again.', 'Exhibit', 'CB12', 'district', 'office', 'see', 'Exhibit', 'MM42.']
我的感觉是某种形式的列表理解可能能够减少列表以仅给出 Exhibit XY 组合,例如像这样:
print([i for i in list_full_text if [some condition])
但我不确定什么条件可以捕获所有“展览”、“X”和“Y”。
注意:文本正文还包含各种数字,例如年份(例如 1992)或数量(例如 50)。我只需要前面有字母的那些。
非常感谢, 伙计
【问题讨论】:
-
你能提供一些 pdfplumber 输出的样例吗?
-
嗨 - 我已将此添加到主帖中。谢谢
标签: python list list-comprehension text-extraction