The quopri.encode()
function in Python performs quoted-printable encoding on data with few non-printable characters. quopri.encode()
converts data with majority US ASCII characters to data with all US ASCII characters.
To use the quopri.encode()
function, the program needs to import the quopri
module as shown below:
import quopri
quopri.encode()
accepts input
, output
, and quotetabs
as non-optional parameters.
input
and output
parameters accept binary file objects. The quopri.encode()
function encodes the input binary file and writes the quoted-printable encoded data to the output file.quotestab
parameter accepts a True
or False
value. The quopri.encode()
function encodes spaces and tabs if the quotetabs
is true; otherwise, it leaves them unencoded.Note: Tabs and spaces at the end of a line are always encoded.
header
parameter accepts a True
or False
value. quopri.encode()
encodes spaces as underscores if the header
is true; otherwise, it leaves them unencoded.Note:
quopri.encode()
usesFalse
as the default value ofheader
.
The quopri.encode()
function encodes all 8-bit value characters into a =
and two hexadecimal digits except printable ASCII characters. For example, =
has a decimal value of 61 and is encoded as =3D
.
All ASCII characters with decimal values from 33 to 126 are printable and decoded as well as encoded as themselves. An exception is =
with an ASCII value of 61.
A space with ASCII value 9 and a tab with ASCII value 32 is encoded as themselves.
The following code demonstrates the use of the quopri.encode()
function:
import quopri from io import BytesIO #Example 1 #example text text = 'This text contains ünicöde' #create an output file outputFile = BytesIO() #create an input file inputFile= BytesIO((text).encode('utf-8')) #Encode quopri.encode(inputFile, outputFile, quotetabs = False) #get content of the encoded file print(outputFile.getvalue()) #Example 2 inputFile2= BytesIO((text).encode('utf-8')) outputFile2 = BytesIO() quopri.encode(inputFile2, outputFile2, quotetabs = True) print(outputFile2.getvalue()) #Example 3 inputFile3= BytesIO((text).encode('utf-8')) outputFile3 = BytesIO() quopri.encode(inputFile3, outputFile3, quotetabs = False, header = True) print(outputFile3.getvalue())
The example text contains two non-printable characters and three spaces.
The program creates input and output files using the BytesIO
module. BytesIO
creates an in-memory binary file-like object. Once created, the program can read from and write to it like a regular file.
The program displays the output through the getValue()
function, which extracts the file’s contents.
The quopri.encode()
function takes the quotetabs
parameter as False
and displays the spaces as themselves as a result. The program encodes Unicode characters ü and ö as =C3=BC and =C3=B6 respectively.
Note:
utf-8
encoding of Unicode characters can take up to four bytes.
The quopri.encode()
function takes the quotetabs
parameter as False
and encodes the spaces as =20 as a result.
The quopri.encode()
function takes the header
parameter as True
and encodes the spaces as _ as a result.
RELATED TAGS
CONTRIBUTOR
View all Courses