【发布时间】:2016-07-18 00:28:51
【问题描述】:
我正在使用 Python 从网上抓取一些 pdf,以便我可以将它们转换为文本文件以便在 R 中进行分析。
我正在使用 pdfminer,然后将它们编码为 utf-8,但完成的文本文件仍然包含许多字节对象的表示形式(例如 '\xe2\x80\x94'),而不是所需的字符本身。
我的查询类似于Why won't Python display this text correctly? (UTF-8 Decoding Issue),不同之处在于我已经在 utf-8 中编码了我的字节对象并且仍然遇到同样的问题。
我的代码如下:
from pdfminer.converter import TextConverter
from io import StringIO
from io import open
from urllib.request import urlopen
def readPDF(pdfile):
rsrcmgr=PDFResourceManager()
retstr=StringIO()
laparams=LAParams()
device=TextConverter(rsrcmgr,retstr,laparams=laparams)
process_pdf(rsrcmgr,device,pdfFile)
device.close()
content=retstr.getvalue()
retstr.close()
return content`
pdfFile=urlopen(webaddress)
outputString=readPDF(pdfFile)
proceedings=outputString.encode('utf-8')
proceedings=str(proceedings)
file=open(filename,"w")
file.write(proceedings)
file.close()
抱歉,如果这很简单。我对 Python 很陌生。
【问题讨论】:
标签: python-3.x pdf unicode utf-8 ascii