我试图用pdf文件中的ASCII表示替换十六进制表示(#…)
import re
with open("C:\\Users\\Suleiman JK\\Desktop\\test\\hello-world-malformed.pdf","rb") as file1:
stuff = file1.read()
stuff = re.sub("#([0-9A-Fa-f]{2})",lambda m:unichr(int(m.groups()[0],16)),stuff)
with open("C:\\Users\\Suleiman JK\\Desktop\\test\\hello-world-malformed.pdf","wb") as file1:
file1.write(stuff)
file1 = open("C:\\Users\\Suleiman JK\\Desktop\\test\\hello-world-malformed.pdf")
print file1.read()
当我使用“Geany”运行它时,会出现以下错误:
^{pr2}$
不要使用
unichr()
;它生成一个包含一个字符的unicode字符串。不要混合使用Unicode字符串和字节字符串(二进制数据),因为这会触发隐式编码或解码。这里触发隐式解码并失败。在代码点的值限制为0-255,因此简单的
chr()
就可以:相关问题 更多 >
编程相关推荐