The quopri Module
The quopri module implements quoted printable encoding, according to
the MIME standard.
This encoding can be used to convert text messages that consist mostly of plain U.S. ASCII text, such as messages written in most European languages, to messages that only use U.S. ASCII. This can be quite useful if you’re sending stuff via steam-powered mail transports to people using vintage mail agents. Example 4-23 demonstrates.
Example 4-23. Using the quopri Module
File: quopri-example-1.py
import quopri
import StringIO
# helpers (the quopri module only supports file-to-file conversion)
def encodestring(instring, tabs=0):
outfile = StringIO.StringIO()
quopri.encode(StringIO.StringIO(instring), outfile, tabs)
return outfile.getvalue()
def decodestring(instring):
outfile = StringIO.StringIO()
quopri.decode(StringIO.StringIO(instring), outfile)
return outfile.getvalue()
#
# try it out
MESSAGE = "å i åa ä e ö!"
encoded_message = encodestring(MESSAGE)
decoded_message = decodestring(encoded_message)
print "original:", MESSAGE
print "encoded message:", repr(encoded_message)
print "decoded message:", decoded_message
original: å i åa ä e ö!
encoded message: '=E5 i =E5a =E4 e =F6!\012'
decoded message: å i åa ä e ö!As Example 4-23 shows, non-U.S. characters are mapped to an equals sign (=) followed by two hexadecimal digits. So it is the equals sign character itself (“=3D”), as well as whitespace at the end of lines (“=20”). Everything else looks just like before. So provided you ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access