The analysis Python uses the Pickle module to complete the function of the enhancement and deletion check.

Source: Internet
Author: User
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# 
Related Article

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.