tre*_*rez 2 python pdf unicode
我正在尝试读取包含以下内容的pdf文件:
%PDF-1.4\n%âãÏÓ
Run Code Online (Sandbox Code Playgroud)
如果我用open读取它,则可以使用,但是如果我尝试使用codecs.open(filename,encoding =“ utf8”,mode =“ rb”)来获取unicode字符串,则会收到以下异常:
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe2 in position 10: invalid continuation byte
Run Code Online (Sandbox Code Playgroud)
您知道从此文件的内容中获取unicode字符串的方法吗?
PS:我正在使用python 2.7
PDF由二进制数据而非文本构成。它们不能有意义地表示为Unicode字符串。
对于它的价值,您可以通过将PDF视为ISO8859-1文本来获取包含这些特殊字符的Unicode字符串:
f = codecs.open(filename, encoding="ISO8859-1", mode="rb")
Run Code Online (Sandbox Code Playgroud)
但是到那时,最好只使用普通open字节并读取字节。Unicode用于文本,而不用于数据。