On this page
quopri
Prev: queue | Up: stdlib_module | Next: random
Rel: email, json, mailbox, mimetypes, base64
Ref: docs.python.org — quopri
Tags: #public #python3 #stdlib
Import
import quopri
Overview
Encoder and decoder for MIME Quoted-Printable transfer encoding (RFC 1521/2045), which keeps mostly-ASCII text human-readable while escaping non-ASCII or control bytes as =XX hex pairs. Use quopri when working directly with low-level email or MIME payloads (though high-level email.message.EmailMessage usually handles this automatically).
Quick Start
import quopri
raw = "Café = 100%".encode("utf-8")
encoded = quopri.encodestring(raw)
assert b"=C3=A9" in encoded and b"=3D" in encoded
assert quopri.decodestring(encoded) == raw
Implementation Walkthrough
1. Streaming Large Payloads and Header Mode
encode(inp, out, quotetabs) and decode(inp, out) stream binary file-like objects, and header=True encodes spaces as underscores (_) per RFC 2047 header rules:
import io
import quopri
src = io.BytesIO(b"hello world\t!")
dst = io.BytesIO()
quopri.encode(src, dst, quotetabs=True)
assert dst.getvalue() == b"hello world=09!"
assert quopri.encodestring(b"a b", header=True) == b"a_b"
Gotchas & Boundaries
- Bytes only:
encodestring()anddecodestring()take and returnbytes, notstr—encode/decode UTF-8 explicitly. - Soft line breaks (
=\n): Quoted-Printable wraps lines at 76 characters by inserting=\n, whichdecodestring()strips cleanly. - Prefer base64 for binary data: Quoted-Printable bloats arbitrary binary blobs by ~3×; use it only for mostly-ASCII text MIME parts.
Common Functions
- quopri.decode() —
decode(input, output, header=False)— Read 'input', apply quoted-printable decoding, and write to 'output'. - quopri.decodestring() —
decodestring(s, header=False)—quopri.decodestring()function. - quopri.encode() —
encode(input, output, quotetabs, header=False)— Read 'input', apply quoted-printable encoding, and write to 'output'. - quopri.encodestring() —
encodestring(s, quotetabs=False, header=False)—quopri.encodestring()function.
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/binascii/a2b_qp
- tuts/python3/stdlib_module/binascii/b2a_qp
- tuts/python3/stdlib_module/queue
- tuts/python3/stdlib_module/quopri/decode
- tuts/python3/stdlib_module/quopri/decodestring
- tuts/python3/stdlib_module/quopri/encode
- tuts/python3/stdlib_module/quopri/encodestring
- tuts/python3/stdlib_module/random
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/queue Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/quopri/decode Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/quopri/decodestring Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/quopri/encode Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/quopri/encodestring Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/random Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/base64 Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/binascii/a2b_qp links to this page; shared tags: #stdlib