且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

编码问题:在Python中解码带引号的可打印字符串

更新时间:2023-09-05 22:41:34

import quopri

编码:

您可以使用quopri.encodestring()将字符'é'编码为Quoted-Printable.它需要一个字节对象,并返回经过QP编码的字节对象.

You can encode the character 'é' to Quoted-Printable using quopri.encodestring(). It takes a bytes object and returns the QP encoded bytes object.

encoded = quopri.encodestring('é'.encode('utf-8'))
print(encoded)

它会打印b'= C3 = A9'(而不是问题中指定的"= AC = E9"或"= A3 = E9")

which prints b'=C3=A9' (but not "=AC=E9" or "=A3=E9" as specified in the question)

解码:

mystring = '=C3=A9'
decoded_string = quopri.decodestring(mystring)
print(decoded_string.decode('utf-8'))

quopri.decodestring()返回一个 bytes 对象,该对象以utf-8编码(可能是您想要的).如果要打印字符'é',请使用.decode()解码 utf-8 编码的字节对象,然后将'utf-8'作为参数传递.

quopri.decodestring() returns a bytes object which is encoded in utf-8(which may be what you want). If you want the character 'é' to be printed, decode utf-8 encoded bytes object using .decode() and pass 'utf-8' as argument.