Back to Python
2026-03-076 min read

Quoted-Printable (Python Programming)

Learn Quoted-Printable (Python Programming) step by step with clear examples and exercises.

Title: Quoted-Printable Decode/Encode in Python Programming (Expanded)

Why This Matters

In web development, data is often transferred using email and newsgroups, where the data may contain special characters that cannot be sent directly. To solve this issue, a method called Quoted-Printable encoding is used to encode these special characters. As a Python developer, understanding and implementing Quoted-Printable encoding can help you work with such data more effectively in various applications like email processing, content management systems, and web scraping.

Prerequisites

To follow this lesson, you should be familiar with the following:

  • Basic Python syntax and functions
  • String manipulation in Python
  • Understanding of ASCII characters
  • Familiarity with file handling (reading/writing files)
  • Knowledge of email headers and content types

Additional Prerequisites

  • Understanding of the difference between text data and binary data
  • Familiarity with Base64 encoding and its limitations for text data
  • Basic understanding of multi-part messages in email

Core Concept

Quoted-Printable encoding is a method used to encode special characters in data that cannot be sent directly. It replaces certain special characters with = followed by the number of spaces required to represent the character, and encloses the encoded data within two dashes (---).

Here's an example of Quoted-Printable encoded data:

From: User <user@example.com>
Subject: Test message

This is a test message with special characters: \n\t!@#$%^&*()_+-=[]{};':"\\|,.<>/?

--_NmP2P34567
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

This is a test message with special characters:\n\t!@#$%^&*()_+-=[]{};':"\|,.<>/?
--_NmP2P34567--

In this example, the special characters have been replaced by = followed by the number of spaces required to represent the character. For instance, the newline character \n has been encoded as =\r\n, where \r\n represents a carriage return and line feed (two ASCII characters used to indicate a new line).

Python provides built-in functions to decode and encode Quoted-Printable data. The base64 module includes the quopri submodule, which contains functions for this purpose.

Decoding Quoted-Printable Data

The decode_quoted_printable() function from the quopri module can be used to decode Quoted-Printable data:

import base64

encoded_data = b'This is a test message with special characters:\n\t!@#$%^&*()_+-=[]{};':"\\|,.<>/?--_NmP2P34567Content-Type: text/plain; charset="us-ascii"Content-Transfer-Encoding: quoted-printableThis is a test message with special characters:\n\t!@#$%^&*()_+-=[]{};':"\|,.<>/?--_NmP2P34567--'
decoded_data = base64.quopri.decode(encoded_data)
print(decoded_data.decode())

Encoding Quoted-Printable Data

To encode data using Quoted-Printable encoding, you can use the encodestring() function from the quopri module:

import base64

original_data = "This is a test message with special characters:\n\t!@#$%^&*()_+-=[]{};':\"\\|,.<>/?"
encoded_data = base64.quopri.encodestring(original_data.encode()).decode().strip().replace('\n', '\r\n').replace('+', ' ')
print(encoded_data)

In this example, we first encode the original data as bytes using encode(). Then, we use the encodestring() function to encode it as a Base64 string with Quoted-Printable line breaks. Finally, we replace '+' with a space and '\n' with '\r\n' to ensure proper formatting for Quoted-Printable data.

Handling Multi-part Messages

When dealing with multi-part messages containing Quoted-Printable encoded data, make sure to handle the boundary marker (---) correctly. Each part of the message should be enclosed between two boundary markers, and the boundary marker used for each part should be unique. When decoding or encoding such messages, ensure that you include the correct boundary marker in your code.

Worked Example

Let's write a Python script that reads a file containing Quoted-Printable encoded data, decodes it, and writes the decoded data to another file:

import base64
import email

def decode_qp(data):
"""Decode Quoted-Printable data."""
decoded = email.header.decode_quoted_printable(data)
return decoded.replace('\r\n', '\n')

def read_and_decode_file(input_file, output_file):
with open(input_file, 'rb') as f:
message = email.message_from_file(f)
for part in message.walk():
if part.get_content_type() == "text/plain":
decoded_data = decode_qp(part.get_payload())
with open(output_file, 'a') as f:
f.write(decoded_data)

Test data (multi-part message)

input_file = "quoted_printable_encoded_multi_part.eml"

output_file = "decoded_quoted_printable.txt"

read_and_decode_file(input_file, output_file)


In this example, we define a function `read_and_decode_file()` that reads the contents of an input file containing Quoted-Printable encoded data (which may be a multi-part message), decodes it using our `decode_qp()` function, and writes the decoded data to an output file.

Common Mistakes

  1. Forgetting to replace \r\n with a newline character (\n) when decoding Quoted-Printable data. This can cause issues with line breaks in the decoded data.
  2. Not properly encoding special characters when using Quoted-Printable encoding. Make sure to replace all special characters that need to be encoded, and enclose the encoded data between two dashes (---).
  3. Using the wrong function for decoding or encoding Quoted-Printable data. Ensure you're using the email.header.decode_quoted_printable and base64.quopri.encodestring functions, respectively.

Common Mistakes (Continued)

  1. Not handling line breaks correctly when encoding or decoding Quoted-Printable data. Make sure to replace '\n' with '\r\n' when encoding and replace '\r\n' with '\n' when decoding.
  2. Assuming that Quoted-Printable encoding can be used for binary data. Remember that Quoted-Printable encoding is meant for text data only, and Base64 encoding should be used for binary data instead.
  3. Not properly handling the boundary marker (---) in Quoted-Printable encoded data when decoding or encoding. Make sure to include the boundary marker correctly when working with multi-part messages.
  4. Not properly escaping special characters within quoted strings in Quoted-Printable encoded data. Remember that special characters inside double quotes should be escaped by doubling them (e.g., "" becomes """).

Practice Questions

  1. Write a Python script that decodes the following Quoted-Printable encoded data:
From: User <user@example.com>
Subject: Test message

This is a test message with special characters:\n\t!@#$%^&*()_+-=[]{};':"\\|,.<>/?

--_NmP2P34567
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

This is a test message with special characters:\n\t!@#$%^&*()_+-=[]{};':"\|,.<>/?
--_NmP2P34567--
  1. Write a Python script that encodes the following data using Quoted-Printable encoding:
This is a test message with special characters:
\n\t!@#$%^&*()_+-=[]{};':"\\|,.<>/?
  1. Write a Python script that reads a file containing multiple Quoted-Printable encoded messages and decodes each message individually, writing the decoded data to separate files.
  2. Write a Python script that handles a multi-part message with Quoted-Printable encoded text parts, decoding each part and saving it to a separate file.

FAQ

  1. What is the difference between Quoted-Printable encoding and Base64 encoding?
  • Quoted-Printable encoding is used to encode special characters in text data while preserving line breaks, whereas Base64 encoding converts binary data into a format that can be safely transmitted over email or newsgroups but does not preserve line breaks.
  1. Why are special characters encoded in Quoted-Printable data using = followed by the number of spaces required to represent the character?
  • Special characters are encoded this way because they cannot be sent directly in email and newsgroup messages. By replacing them with = followed by the number of spaces required to represent the character, the data can still be read as text, even though it has been encoded.
  1. Can I use Quoted-Printable encoding for binary data?
  • No, Quoted-Printable encoding is meant for text data only. For binary data, you should use Base64 encoding instead.
  1. What happens if a special character in Quoted-Printable encoded data has an equal sign (=) as part of its representation?
  • If a special character in Quoted-Printable encoded data has an equal sign (=) as part of its representation, it can be represented using two equal signs (==). For example, the space character is represented as =20. To encode a space character followed by an equal sign, you would use ===20.
  1. How do I handle multi-part messages with Quoted-Printable encoding?
  • When dealing with multi-part messages containing Quoted-Printable encoded data, make sure to handle the boundary marker (---) correctly. Each part of the message should be enclosed between two boundary markers, and the boundary marker used for each part should be unique. When decoding or encoding such messages, ensure that you include the correct boundary marker in your code.
  1. How do I escape special characters within quoted strings in Quoted-Printable encoded data?
  • Special characters inside double quotes should be escaped by doubling them (e.g., "" becomes """). This ensures that the special characters are correctly interpreted when the data is decoded.
Quoted-Printable (Python Programming) | Python | XQA Learn