unordered modules
What is an unordered module, which converts a string into a 16-binary string type, is mainly used in network programming.
1,json Module
1, for network transmission, cross-language, available types have an int str list-the "meta-ancestor will become a list dictionary-" double quotation marks
2, Cons: limited data types supported
1.json.dumps ()
Converts a character into a bytes type, used to transmit
2.json.loads ()
Read bytes Type
dic = {' Sword Saint ': ' My sword is Your sword ', ' nerf ': ' Shing, there is no life ', ' Prince ': ' Make me a de-state, though far will punish '}lol = Json.dumps (dic,ensure_ascii=false) # Dumps in network programming converts a string to a 16-binary string type print (LOL) ret = json.loads (lol) #用来解读十六进制print (ret)
3.json.dump ()
File writes but requires dumps when the file is written to more data
4.json.load ()
File content Read
f = open (' 233 ', ' a ', encoding= ' Utf-8 ') json.dump (dic,f) #文件写入f. Close () f1 = open (' 233 ', ' R ', encoding= ' Utf-8 ') json.loads ( F1) #文件读取f. Close ()
2.pickle Module
1. Use between Python platforms, easy to use
1,pickle.dumps
2.pickle.loads
3.pockle.dump
4.pockle.load
dic = {' Sword Saint ': ' My sword is Your sword ', ' nerf ': ' Shing, there is no life ', ' Prince ': ' I am a de-state, although far will punish '}dic1 = ' Sword Saint ': ' My sword is Your sword ', ' nerf ': ' The Shing of the chi, there is no life ', ' Prince ': ' guilty of my German state, '}dic2 = ' Sword Saint ': ' My sword is Your sword ', ' nerf ': ' Shing, there is no life ', ' Prince ': ' I am a de-state, although far will punish '}dic3 = ' Sword Saint ': ' My sword is Your sword ', ' nerf ': ' The Shing of the chi, there is no life ', ' Prince ': ' If you make me a German, I will punish you.}k = Pickle.dumps (dic) print (k) L = pickle.loads (k) Print (l) F = open (' 233 ', ' ab ') ret = Pickle.dump (dic,f) F.close () F1 =open (' 233 ', ' RB ') print (Pickle.load (F1)) F1.close () f=open (' 233 ', ' WB ') pickle.dump (dic,f) Pickle.dump ( DIC1,F) Pickle.dump (dic2,f) pickle.dump (dic3,f) f.close () F1 =open (' 233 ', ' RB ') while True: try: print ( Pickle.load (F1)) except Eoferror: breakf.close () class A: def __init__ (self,name,age): self.name = Name self.age = Agea = A (' Alex ', $) k=pickle.dumps (a) #读取所有属性, serializes the properties of a object L=pickle.loads (k) Print (l.__ DICT__)
3.shelve Module
Proprietary modules, only read calls to files.
f["] Mode
4.hashlib Module
Two kinds of application
1, in the file verification above, the transmitter and receiver files are consistent
2, password encryption in a variety of encryption methods:
1.MD5 2.sha Encryption method
1, file consistency check after the file upload network, in order to verify the uploaded files and after the file received by others are consistent, the use of MD5 encryption in Hashlib, verify that both sides of the price encryption after the password is consistent, even if the file has a different space will make the encrypted MD5 password is not the same. 2, ciphertext authentication time encryption MD5 encryption method , General encryption method Sha file encryption method, Password = input (' >>>> ') k = Hashlib.md5 () k.update ( Password.encode (' Utf-8 ')) m = Hashlib.md5 () m.update (' 233 '. Encode (' Utf-8 ')) if k.hexdigest () = = M.hexdigest (): Print (' 666 ') Else: print (' 2333 ')
Python unordered module, hashlib module