[英]How do I decode an image/pdf file properly in python3
I'm sending a file as follows. 我正在发送文件,如下所示。
with open(file,'rb') as f:
rh=f.read()
Now to send it I'm using 现在发送给我
sock.sendto(rh, (ip,port))
While receiving I'm trying to decode this as follows. 接收时,我尝试按以下方式对此进行解码。
dat = sock.recvfrom()
data=dat.decode('ascii')
Then I'm writing this to a file. 然后,我将其写入文件。 This works perfectly fine in case of a text file. 在文本文件的情况下,这工作得很好。 But If I'm sending and receiving a image/pdf file, I get this error .. 但是,如果我发送和接收图像/ pdf文件,则会收到此错误..
File "code.py", line 16, in <module>
data=dat.decode('ascii')
UnicodeDecodeError: 'ascii' codec can't decode byte 0x89 in position 41: ordinal not in range(128)
I'm not able to find out what's the problem here. 我无法在这里找出问题所在。 I have also used dat.decode('utf-8') but no use 我也用过dat.decode('utf-8')但没用
Try opening the file using codecs
. 尝试使用codecs
打开文件。
import codecs
f = codecs.open(filepath, encoding="ISO8859-1")
Worked for me when I was uploading a PDF file using an API. 当我使用API上传PDF文件时为我工作。
There's no need to decode. 无需解码。 You read and sent the file in binary mode, so write the received file in binary mode. 您以二进制模式读取和发送文件,因此以二进制模式写入接收的文件。 encode
/ decode
is meant for translating Unicode text to and from a binary representation. encode
/ decode
用于将Unicode文本与二进制表示形式相互转换。
with open(output_filename,'wb') as f:
f.write(data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.