哪种类型的编解码器最适合对二进制文件进行编码,然后将哪些文件上传到用Python 3写的应用程序中?
I know it's a late answer, and the question isn't entirely clear, but if you need to encode a string of bytes to be saved somewhere, and for whatever reason you can't simply save the binary data, you should encode the string using the base64
module. For example, say you have a long string of bytes (in this case, generated with os.urandom(100)
:
import base64
import os
some_bytes = os.urandom(100)
print(repr(some_bytes))
some_bytes_base64 = base64.standard_b64encode(some_bytes).decode()
print(some_bytes_base64)
This prints something like the following:
b'\xd3\xa3\x0eT/L\xc6\x88\x98\xd0i\xbaa\xb2\x18\x1bx\xde\xf6Zq\xbe\xd9\xb4D\x19\x14\x91Z}wg?\x90\xd7f\xca\x1bxX\xa8\x99\xe6\xbb\xe0\x80\xa3\xc4\xb8\x1f\xfcp\xbc\x8d:/\xcfk\x01\xee\xc1\xde\xdc\xc3\xfa\x0e|7\x8eYt\xd1\x0b\x8a\x89\x9c^\xcf\xbc\\\x00_\x89dyb\x13\xa8\xdb\xba\xe3\x85X\x8d\x1a\x8bz\xe5\xfb\r'
06MOVC9MxoiY0Gm6YbIYG3je9lpxvtm0RBkUkVp9d2c/kNdmyht4WKiZ5rvggKPEuB/8cLyNOi/PawHuwd7cw/oOfDeOWXTRC4qJnF7PvFwAX4lkeWITqNu644VYjRqLeuX7DQ==
As you can see, the string has been properly encoded. The decode()
method converts the byte literal that base64.standard_b64encode
normally returns to a standard string. This answer describes byte literals well, in my opinion.
This should allow you to transmit a file, sequence of bytes, etc. without problems. (After all, isn't that was Base64 encoding
is for?)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.