Cryptographic &json&stringio Module &bytesio module

Source: Internet
Author: User

First, encryption

Cryptographic MD5 RSA Hashlib ( for Python2)

Case one:
Import Hashlib
# m = hashlib.md5 ()
# src = "123456"
# m.update (SRC)
# Print (M.hexdigest ())

M3 = hashlib.md5 ("123456". Encode ("Utf-8"))
src = bytes ("Ling", encoding= "Utf-8")
M3.update (SRC)
Print (M3.hexdigest ())

Execution results

Second, Stringio module &bytesio module

1.StringIO Module
In peacetime development process, sometimes we may not need to write in the file, we can directly through the Stringio module directly to the system memory, if not, can be directly cleared. Stringio is primarily used to write strings in memory, and to cache strings. The interface of his interface and the file operation is consistent, basic all the methods of the file can be used, but only accept the Unicode type of write

Case Two
From IO import Stringio, Bytesio

Stringio = Stringio ()
Stringio.write ("Hello World")
Stringio.write ("Lalalalla, Wo shi mai bao de xiao Hang Jia")
Print (Stringio.getvalue ())
Stringio.truncate (0)
Print (Stringio.getvalue ())
Execution results

1.2 Initializes a stringio with a string.

Case THREE:

From IO import Stringio
output = Stringio (U ' HELLO \nworldl \nling \njing\n ')
# Print (Output.read ())
Print (' aaaaaaaaaaaaaaaaaaa ')
While 1:
s = Output.readline ()
if s = = ':
Break
Print (S.strip ())
Execution Result:

2.bytesIO Module

Stringio operation can only be str, if you want to manipulate binary data, you need to use Bytesio

Third, the JSON module

Json, full name JavaScript Object Notation, is a lightweight data interchange format. The most extensive application of JSON is the data format for the communication of Web servers and clients in Ajax. It is also often used in HTTP requests, so it is natural to learn a variety of JSON. And let's talk about the two ways we usually use the longest in our work.

Dumps and loads with S is the string dump and load without S is processing the file

Loads converting a JSON string to a Python object a lot of what we have is a JSON string that we have to convert to a Python object before we can use the

Case Four,

Import JSON
A = Dict (hello= "Hello")
Print (a)
Print (a["Hello"])
Print (str (a))
Print (Json.dumps (A, ensure_ascii=false))
Execution results

Case FIVE:

Import Codecs
Import JSON

Test = {"A": 1, "B": 2}
With Codecs.open ("1.txt", "W") as F:
Json.dump (test, F)

With Codecs.open ("1.txt", "R") as F:
AA = Json.load (f)
Print (AA)
Print (Type (AA))
Execution Result:

Cryptographic &json&stringio Module &bytesio module

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.