python2 program defaults to the ASCII data type, it is necessary to decode the first data (decode) be in Unicode, and then re-encoding (encode) becomes the data type of conversion desired (gbk, utf-8, gb18030, gb2312), and then decodes become corresponding data type displayed on the screen;
Python3 codec
python3 program default data type is the Unicode, it encodes the data directly (encode) becomes the data type (gbk, utf-8, gb18030, gb2312) want to convert, and then decoded into corresponding data type displayed on the screen.
base64
Base64 encoding is a "gentleman not anti-anti-villain" encoding. Widely used in MIME protocol, e-mail as a transmission coding, coding to generate reversible, after one or two may have "=" ascii character codes are generated.
For python2 Thus, the codec relatively easier. python3 because the conversion from Unicode moment, relatively few problems. Everything see example below:
Python2
def b64encode(s, altchars=None):
"""Encode a string using Base64.
s is the string to encode. Optional altchars must be a string of at least
length 2 (additional characters are ignored) which specifies an
alternative alphabet for the '+' and '/' characters. This allows an
application to e.g. generate url or filesystem safe Base64 strings.
The encoded string is returned.
"""
# Strip off the trailing newline
encoded = binascii.b2a_base64(s)[:-1]
if altchars is not None:
return encoded.translate(string.maketrans(b'+/', altchars[:2]))
return encoded
b64decode source
def b64decode(s, altchars=None):
"""Decode a Base64 encoded string.
s is the string to decode. Optional altchars must be a string of at least
length 2 (additional characters are ignored) which specifies the
alternative alphabet used instead of the '+' and '/' characters.
The decoded string is returned. A TypeError is raised if s is
incorrectly padded. Characters that are neither in the normal base-64
alphabet nor the alternative alphabet are discarded prior to the padding
check.
"""
if altchars is not None:
s = s.translate(string.maketrans(altchars[:2], '+/'))
try:
return binascii.a2b_base64(s)
except binascii.Error, msg:
# Transform this exception for consistency
raise TypeError(msg)
It s It is an object inside a string type.
import base64
s = 'Hello, python'
b = base64.b64encode(s)
print 'b为:', b
base64.b64decode = C (B)
Print 'is C:', c
The Output #
b is: SGVsbG8sIHB5dGhvbg ==
c is: Hello, python
Python3
def b64encode(s, altchars=None):
"""Encode the bytes-like object s using Base64 and return a bytes object.
Optional altchars should be a byte string of length 2 which specifies an
alternative alphabet for the '+' and '/' characters. This allows an
application to e.g. generate url or filesystem safe Base64 strings.
"""
encoded = binascii.b2a_base64(s, newline=False)
if altchars is not None:
assert len(altchars) == 2, repr(altchars)
return encoded.translate(bytes.maketrans(b'+/', altchars))
return encoded
b64decode source
def b64decode(s, altchars=None, validate=False):
"""Decode the Base64 encoded bytes-like object or ASCII string s.
Optional altchars must be a bytes-like object or ASCII string of length 2
which specifies the alternative alphabet used instead of the '+' and '/'
characters.
The result is returned as a bytes object. A binascii.Error is raised if
s is incorrectly padded.
If validate is False (the default), characters that are neither in the
normal base-64 alphabet nor the alternative alphabet are discarded prior
to the padding check. If validate is True, these non-alphabet characters
in the input result in a binascii.Error.
"""
s = _bytes_from_decode_data(s)
if altchars is not None:
altchars = _bytes_from_decode_data(altchars)
assert len(altchars) == 2, repr(altchars)
s = s.translate(bytes.maketrans(altchars, b'+/'))
if validate and not re.match(b'^[A-Za-z0-9+/]*={0,2}$', s):
raise binascii.Error('Non-base64 digit found')
return binascii.a2b_base64(s)
There is a s-bytes object, the first character string encoded encode (). After b64encode / b64decode result is returned bytes object, so we have to be converted to Unicode objects will then use the decode () method to decode.
import base64
s = 'Hello, Python!'
b = base64.b64encode(s.encode('utf-8')).decode('utf-8')
print(b)
c = base64.b64decode(b.encode('utf-8')).decode('utf-8')
print(c)
# output
SGVsbG8sIFB5dGhvbiE=
Hello, Python!