quopri.decode() function in Python performs quoted-printable decoding on data with few non-printable characters.
To use the
quopri.encode() function, the program needs to import the
quopri module as shown below:
The function accepts
output as non-optional parameters.
outputparameters accept binary file objects. The
quopri.decode()function decodes the input file and writes the quoted-printable decoded data to the output file.
headerparameter accepts a
Falsevalue. The function decodes spaces as underscores if the
True; otherwise, it leaves them unencoded.
Falseas the default value of
quopri.decode() function decodes all 8-bit value characters encoded into a
= and two hexadecimal digits, except printable ASCII characters. For example,
=3D has a decimal value of 61 and is decoded as
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 can be encoded as themselves.
The following code demonstrates the use of the
import quopri from io import BytesIO #-----------ENCODING------------- #example text text = 'This text contains ünicöde' #create binary input file inputFile= BytesIO((text).encode('utf-8')) #create binary output file outputFile = BytesIO() #generate encoding quopri.encode(inputFile, outputFile, quotetabs = True) #extract file content print("-----------ENCODING-------------") print(outputFile.getvalue()) #----------DECODING-------------- #create binary output file outputFile2 = BytesIO() #create binary input file inputFile2 = BytesIO(b'This=20text=20contains=20=C3=BCnic=C3=B6de') #generate decoded data quopri.decode(inputFile2, outputFile2) #extract output output = outputFile2.getvalue() #extract text print("-----------DECODING--------------") print(output.decode('utf-8'))
The example text contains two non-printable characters and three spaces.
The program creates input and output files using the
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.
Trueduring encoding. The program encodes Unicode characters ü and ö as =C3=BC and =C3=B6 respectively.
utf-8encoding of Unicode characters can take up to four bytes.
View all Courses