Module use (MD5 encryption, Stringio, JSON)

Source: Internet
Author: User
Tags md5 encryption

MD5 encryption
        在python3的标准库中,已经移除了md5,而关于hash加密算法都放在hashlib这个标准库中,如SHA1、SHA224、SHA256、SHA384、SHA512和MD5算法等

Hex has 16 binary meaning in English, hexdigest () so the method is to convert the data in the hash into data, which contains only 16 binary digits
Use encode to prevent garbled characters when encrypting Chinese
Mode one: for 123456ling encryption
Import Hashlib
M3 = HASHLIB.MD5 ()
src = bytes ("123456ling", encoding= "Utf-8")
M3.update (SRC)
Print (M3.hexdigest ())

Mode Two: 123456 This is a random number, through encryption, then the Ling encryption, to prevent the cracked
Import Hashlib
M3 = hashlib.md5 ("123456". Encode ("Utf-8"))
src = bytes ("Ling", encoding= "Utf-8")
M3.update (SRC)
Print (M3.hexdigest ())

Way three:

Stringio use

Function: often read and write to the file Io, now put in memory, improve performance
Stringio are often used to cache strings because some of the interfaces and file operations of Stringio are consistent, meaning that the same code can act as either a file operation or a Stringio operation.

From IO import Stringio, Bytesio

Stringio = Stringio ()
Stringio.write ("Hello world\n") #写入内存中
Stringio.write ("Lalalalla, Wo shi mai bao de xiao Hang Jia")
Print (Stringio.getvalue ()) #查看值
Stringio.truncate (0) #截断清空
Print (Stringio.getvalue ())

Json
        json模块提供了一种很简单的方式来编码和解码json格式的数据,其中两个主要的函数是json.dumps()和json.loads(),当然与之对应的还要json.dump()和json.load()函数

Method:

loads 把字符串-》python对象dumps 把python对象-》 字符串load 把文件-》 python对象dump 把python对象 写入文件

Method One: Json.dumps ()

Convert a python data structure into JSON:

import jsondata ={   ‘name‘: ‘thinkgamer‘,   ‘age‘: 23,   ‘sex‘: ‘men‘}print(json.dumps(data))print(type(json.dumps(data)))  print(type(data))结果:import jsondata ={   ‘name‘: ‘thinkgamer‘,   ‘age‘: 23,   ‘sex‘: ‘men‘}print(json.dumps(data))print(type(json.dumps(data)))  print(type(data))

Method Two: Json.loads ()

import jsondata ={   ‘name‘: ‘thinkgamer‘,   ‘age‘: 23,   ‘sex‘: ‘men‘}str_tmp=(json.dumps(data))print(json.loads(str_tmp))print(type(json.loads(str_tmp)))结果:{‘name‘: ‘thinkgamer‘, ‘age‘: 23, ‘sex‘: ‘men‘}<class ‘dict‘>

Method Three: Json.dump () writes an object to a file
Import JSON
Data ={
' Name ': ' Thinkgamer ',
' Age ': 23,
' Sex ': ' Men '
}
With open (' User.txt ', ' W ') as FD:
Json.dump (DATA,FD)

Mode four: Json.load () read from file
Import JSON
Data ={
' Name ': ' Thinkgamer ',
' Age ': 23,
' Sex ': ' Men '
}
With open (' User.txt ') as FD:
Date_json=json.load (FD)
Print (Date_json)
Print (Type (Date_json))

Results:
{' name ': ' Thinkgamer ', ' age ': at all, ' sex ': ' Men '}
<class ' Dict ' >

Python2 in the Environment:
In [143]: Import JSON

In [144]: a = dict (hello= "Hello")

In [145]: print (a)
{' Hello ': ' \xe4\xbd\xa0\xe5\xa5\xbd '}

In [146]: Print (a["Hello"])
How are you doing

In [147]: Print (str (a))
{' Hello ': ' \xe4\xbd\xa0\xe5\xa5\xbd '}

In [148]: Print (Json.dumps (A, ensure_ascii=false))
{"Hello": "Hello"}

Module use (MD5 encryption, Stringio, JSON)

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.