0

我正在尝试计算从 PDF 中提取的一系列单词,但我只得到 0 并且它不正确。

total_number_of_keywords = 0
pdf_file = "CapitalCorp.pdf"
tables=[]

words = ['blank','warrant ','offering','combination ','SPAC','founders']
count={} # is a dictionary data structure in Python


with pdfplumber.open(pdf_file) as pdf:
    pages = pdf.pages
    for i,pg in enumerate(pages):
        tbl = pages[i].extract_tables()
        for elem in words:
            count[elem] = 0
        for line in f'{i} --- {tbl}' :
            elements = line.split()
            for word in words:
                count[word] = count[word]+elements.count(word)
print (count)
        
4

1 回答 1

0

这将完成这项工作:

import pdfplumber
pdf_file = "CapitalCorp.pdf"
words = ['blank','warrant ','offering','combination ','SPAC','founders']

# Get text
text = ''
with pdfplumber.open(pdf_file) as pdf:
    for i, page in enumerate(pdf.pages):
        text = text+'\n'+str(page.extract_text())

# Setup count dictionary
count = {}
for elem in words:
    count[elem] = 0
        
# Count occurences
for i, el in enumerate(words):
    count[f'{words[i]}'] = text.count(el)

首先,您将 PDF 的内容存储在变量 中text,该变量是一个字符串。

然后,您设置count字典,每个元素都有一个键,words并将相应的值设置为 0。

最后,您使用该方法计算wordsin的每个元素的出现次数,并将其存储在字典的相应键中。textcount()count

于 2021-10-08T09:05:04.617 回答