The quopri and base64 modules are both used for encoding and decoding, and base64 and quoted-printable are both common encodings in emails.
Quoted-printable: The English character is not processed except =. The other characters are encoded as = The hexadecimal number of the two bytes that contain this character. "=/R/N" is available at the end of the row ".
The quopri module only needs to use its encode, decode, encodestring, and decodestring. The first two are encoding and decoding of files (data encoding and decoding in stringio ), the latter two are encoded and decoded strings.
Take a look at the following example: #-*-encoding: gb2312 -*-
Import quopri
A = "only a test data"
B = quopri. encodestring (a) # encode the string
Print B
Print quopri. decodestring (B) # decode the string
Import stringio
C = stringio. stringio ()
D = stringio. stringio ()
E = stringio. stringio ()
C. Write ()
C. Seek (0)
Quopri. encode (c, d, 0) # encode the data in stringio. The third parameter 0 indicates that the space and tab characters are not encoded. The value 1 indicates that the encoding is performed.
Print D. getvalue ()
D. Seek (0)
Quopri. Decode (d, e) # decode the data in stringio
Print E. getvalue ()
F1 = open ("aaa.txt", "W ")
F1.write ()
F1.close ()
F1 = open ("aaa.txt", "R ")
F2 = open ("bbb.txt", "W ")
Quopri. encode (F1, F2, 0) # upload data from aaa.txt to bbb.txt
F1.close ()
F2.close ()
Print open ("bbb.txt", "R"). Read ()