Concept:
Common Syntax: dump: Installs the object status in the current memory on the hard disk file.
Common Syntax: load: Import objects in disk files to memory
Exercises:
Store account information in the dictionary, use pickle dump to the file, and load to modify the dictionary
[[email protected] python_scripts]# cat 15pickle_dump.py #!/usr/bin/pythonimport pickleaccount_info={ ‘01‘:[‘mima‘,15000,15000], ‘02‘:[‘mima‘,2000,2000]}f=open(‘acc.txt‘,‘wb‘)pickle.dump(account_info,f)f.close()
[[email protected] python_scripts]# cat 16pickle_load.py #!/usr/bin/pythonimport picklef=open(‘acc.txt‘,‘r‘)info=pickle.load(f)f.close()info[‘02‘][1]=20000f=open(‘acc.txt‘,‘w‘)pickle.dump(info,f)f.close()
[[Email protected] python_scripts, draw 50 RMB ['mima', 1950,200 0] [[[email protected] python_scripts] # Python 16pickle_load.py -- spend 50 RMB ['mim ', 1900,200 0] [[email protected] python_scripts] # Python 16pickle_load.py -- spend 50 yuan ['mim ', 1850,200 0] [[email protected] python_scripts] # Python 16pickle_load.py -- spend 50 yuan ['mim', 1800,200 0]
11. Pickle serialization