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!12'
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 don't use too many weird characters, the encoded string is nearly as readable as the original.

(Europeans generally hate this encoding and strongly believe that certain U.S. programmers deserve to be slapped in the head with a huge great fish to the jolly music of Edward German....)

Core Modules

More Standard Modules

Threads and Processes

Data Representation

File Formats

Mail and News Message Processing

Network Protocols

Internationalization

Multimedia Modules

Data Storage

Tools and Utilities

Platform-Specific Modules

Implementation Support Modules

Other Modules



Python Standard Library
Python Standard Library (Nutshell Handbooks) with
ISBN: 0596000960
EAN: 2147483647
Year: 2000
Pages: 252
Authors: Fredrik Lundh

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net