This article mainly introduced the Python uses the Pickle module realizes "the Adding and deleting" the simple function, has the certain reference value, the interest small partner may consult.
The role of Pickle:
1:pickle.dump (dict,file) converted the dictionary into binary file.
2:pickle.load (file) to convert the binary contents of files into a dictionary
Import pickle# def adds (): users = {"name": "Yangbin", "Age": $, "sex": "Male"} with open ("Red.txt", "WB") as F:
pickle.dump (Users, f) dic = {} with open ("Red.txt") as SD: dic = pickle.load (SD) print dic# Delete def delet ES (): dic = {} with open ("Red.txt") as f: dic = Pickle.load (f) dic.pop ("Sex") with open ("Red.txt "WB") as FF: pickle.dump (DIC, ff) print dic # change def changes (): dic = {} with open ("Red.txt") as f:< C16/>dic = Pickle.load (f) dic["age"] =-with open ("Red.txt", "WB") as F: Pickle.dump (DIC, F) Print dic# def finds (): dic = {} with open ("Red.txt") as f: dic = Pickle.load (f) to k,v in Dic.item S (): print "%s--->%s"% (k, v) adds () deletes () changes () finds ()
Operation Result:
root@python3:/python/python2/linshi# python 01.py {' age ': $, ' name ': ' Yangbin ', ' sex ': ' Male '} {' Age ': ' ' name ': ' Yangbin '} {' age ': ' Name ': ' Yangbin '}age---> 28name---> Yangbinroot@python3:/python /python2/linshi#