5.1 json
JS objects
teacher_1 = {var
name: 'juhao',
Age: 18 is, Feature: [ 'high', 'rich', 'handsome'] }
JSON string
{
"Name": "juhao",
"Age": 18 is,
"Feature": [ 'high', 'rich', 'handsome'] }
Python dictionary
{
'Name': 'juhao',
'Age': 18 is 'Feature': [ 'high', 'rich', 'handsome'] }
note
1. The string must be enclosed in double quotes (ie: "") to include the 2 may be a string, numeric, true, false, null, list, or dictionary.
-
1. The python data into json:
json.dumps (obj) Indent indent achieved (typically. 4), json.dumps (obj, indent =. 4) ensure_ascii ascii whether to use analytical ensure_ascii = False
-
2. python json data into data:
2.json.loads(s)
-
3. converted to json and saved to a file
3.json.dump(obj, fp)
-
4. JSON read from a file, and data into python
4.json.load(fp)
with open ('json_test','w+')as f:
json.dump(obj,indent = 4,ensure_ascii = False,fp=f)
with open ('json_test','r+')as f:
res = json.load(fp=f)
print res
5.2 hashlib, base64 module
1.hashlib (MD5)
hash algorithm is - the kind of data of arbitrary length into a fixed-length function data
-
1. Symmetric encryption:
Data encryption and decryption using the same key
-
2. Asymmetric encryption:
Encryption and decryption using two different keys, a public key used to encrypt the data, the private key is used to decrypt the data
-
3-way encryption:
Only encrypt data, not decrypt the data
2.Hash structure:
Irreversible: Unable to restore the original data from the hash value of the fixed-length output: No matter how long the original data input, the result is the same length anti-modifying properties: small changes in input, even if only one character will cause results major changes and strong impact resistance: the same hash value is difficult to find two different data content, so that they have the almost impossible
Hashlib module provides a number of hash algorithms are:
1.Md5
2.SHA series: sha1, sha224, sha256, sha384 , sha512
api description:
hashlib.new (name, data = b " ) to generate a hash objects
hashlib.hash algorithm name (data = b") to generate a target using the hash algorithm
Hash object .update (arg = None) to update the object hash
Hash object .digest () return value (bytes type) obtained Hash algorithm Hash object .hexdigest () return value (str type) Hash algorithm obtained
import hashlib
result = hashlib.new('md5','南北'.encode())
result = hashlib.md5('南北'.encode())
print(result.digest())
print(result.hexdigest())
result = hashlib.md5()
result.update('南北'.encode())
print(result.digest())
print(result.hexdigest())
3.base64
64 is a base64 characters with a method of representing arbitrary binary data (binary data encoded into ASCII characters) using AZ, az, 0-9, +, / 64 characters which
Features:
Used to convert the data into a non-ASCII characters in the ASCII character is a method used to encode the URL of the binary data can be converted to non-printed printable string
api description:
base64.b64encode (s) of the binary data base64 encoding
base64.b64decode (s) decodes the encoded data by base64 base64.urlsafe_b64encode (s) a URL base64 encoding base64.urlsafe_b64decode (s) decodes
import base64
data = '爱我'
res = base64.b64encode(data.encode)
import json
import base64
data={
'name':'nanbei',
'age':10,
'feature':["gao","fu","shuai"]
}
with open('my_json,txt','w')as f:
json.dump(data,f,ensure_ascii=False)
with open('my_json,txt','r')as f:
result=json.load(f)
print(type(result))
print(result['feature'][2])
with open('my_json,txt','rb')as f:
result=base64.b64encode(f.read())
with open('my_json,txt','wb')as f:
f.write(result)
with open('my_json,txt','rb')as f:
print(base64.b64decode(f.read()).decode())