我正在使用PyPDF2包从.pdf文件中提取文本。我得到了输出,但不是它想要的形式。我找不到问题出在哪里?
代码片段如下:
import PyPDF2
def Read(startPage, endPage):
global text
text = []
cleanText = " "
pdfFileObj = open('F:\\Pen Drive 8 GB\\PDF\\Handbooks\\book1.pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
num_pages = pdfReader.numPages
print(num_pages)
while (startPage <= endPage):
pageObj = pdfReader.getPage(startPage)
text += pageObj.extractText()
startPage += 1
pdfFileObj.close()
for myWord in text:
if myWord != '\n':
cleanText += myWord
text = cleanText.strip().split()
print(text)
Read(3, 3)我现在得到的输出是作为参考的,如下所示:

任何帮助都是非常感谢的。
发布于 2018-08-27 22:48:37
cleanText += myWord这一行只是将所有单词连接成一个长字符串。如果您想要过滤'\n',而不是:
for myWord in text:
if myWord != '\n':
cleanText += myWord
text = cleanText.strip().split()您可以这样做:
text = [w for w in text if w != '\n']https://stackoverflow.com/questions/52041720
复制相似问题