Python module base64

Source: Internet
Author: User

The base64 module only uses eight methods: encode, decode, encodestring, decodestring, b64encode, b64decode, urlsafe_b64decode, and urlsafe_b64encode. The eight of them can be divided into four groups: encode and decode, which are used to encode and decode files. They can also perform encoding and decoding on the data in stringio; encodestring and decodestring, this function is used to encode and decode strings. b64encode and b64decode are used to encode and decode strings and replace symbol characters. This function is like this: Because the base64 encoded character Division
The English letters and numbers are also three characters +/=, where = is only used to complete the encoding after the number of characters is 4 integers, and + and/must be replaced in some cases. b64encode and b64decode provide such a function. When + and/need to be replaced, the most common is the base64 encoding of the URL. Urlsafe_b64encode and urlsafe_b64decode are used to encode and encode the URL in base64. in fact, they are also called by the previous functions.
The following is an example:

#-*-Encoding: gb2312 -*-
Import base64
Import stringio

A = "this is a test"
B = base64.encodestring (a) # encode the string
Print B
Print base64.decodestring (B) # decodes a string

C = stringio. stringio ()
C. Write ()
D = stringio. stringio ()
E = stringio. stringio ()
C. Seek (0)
Base64.encode (c, d) # encode the data in stringio
Print D. getvalue ()
D. Seek (0)
Base64.decode (d, e) # decode the data in stringio
Print E. getvalue ()

A = "This Is A + test"
B = base64.urlsafe _ b64encode (a) # encode the URL string
Print B
Print base64.urlsafe _ b64decode (B)

The above encode function and decode function parameters can also be file objects, which is like this:

F1 = open('aaa.txt ', 'R ')
F2 = open('bbb.txt ', 'w ')

Base64.encode (F1, F2)

F1.close ()
F2.close ()

###########

Easy to use

>>> a='123'>>> a'123'>>> b=a.encode("hex") >>> b'313233'>>> b.decode("hex")  '123'
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.