One, JSON & Pickle & Shelve Module
JSON, used to convert between string and Python data types
Pickle for conversion between Python-specific types and Python data types
The JSON module provides four functions: dumps, dump, loads, load
The Pickle module provides four functions: dumps, dump, loads, load
JSON module:
Here are some examples to learn:
First, let's look at the JSON dumps and loads methods.
s1={"K1": "V1"}st=json.dumps (S1) print (St,type (ST)) s= ' {"K1": "v1"} ' dic=json.loads (s) print (Dic,type (DIC))
Output results
{"K1": "v1"} <class ' str ' >{' K1 ': ' v1 '} <class ' Dict ' >
You can see that the JSON dumps method converts the data to a character type when the data is processed, and loads restores its type again.
Then look at the dump and load methods of JSON, with examples to understand:
Li=[11,22,33]li=json.dump (Li,open (' db ', ' W ')) li=json.load (open (' db ', ' R ')) print (Li,type (LI))
The difference between JSON modules dumps, loads, load, dump:
Load,dump can load external files, process file data, dumps,loads mainly handle in-memory data
Pickle module:
Let's take a look at Pickle's dumps and loads methods, which we'll look at by example:
Import Picklei=[11,22,33]r=pickle.dumps (LI) print (R) result=pickle.loads (r) print (result)
The result is:
B ' \x80\x03]q\x00 (k\x0bk\x16k!e. ' [11, 22, 33]
The Pickle Dupms method saves the data as a pickle-specific data type
Looking at the dump and load methods of pickle, let's take a look at the example:
Import Picklei=[11,22,33]pickle.dump (I,open (' db ', ' WB ')) result=pickle.load (open (' db ', ' RB ')) print (result)
It is important to note that the dump file or the load file is required to use the binary.
Shelve module
Shelve is a simple data storage scheme, he has only one function is open (), the function receives a parameter is the file name, and then return a shelf object, you can use him to store things, you can simply think of him as a dictionary, when you are stored, Call the close function to close it.
Or come up with an example to understand:
f = shelve.open (' user.db ', ' WC ') f[' baidu '] = ' www.baidu.com ' f[' qq '] = ' www.qq.com ' f[' a ', ' = ' www.360.cn ' f.close () F = Shelve.open (' user.db ', ' A + ') print (f[' Baidu '],f[' QQ '],f[' 360 ')
The result is:
www.baidu.comwww.qq.comwww.360.cn
To update the shelve serialized data, learn by example:
F=shelve.open (' user_db ', ' C ') f["user"]={"Digital Appliance": {"printer": "3600", "Phone": "3800", "Computer": "8000", "Camera": "10000"}, "clothing Department" : {"Instant noodles": "4", "Jacket": "300", "Jeans": "288", "King Lao Ji": "6"}, "cosmetics": {"Korean bundle": "388", "European Poetry": "666", "L ' oreal": "888", "Hundred Sparrow": "259"}, "Car": {"pashat": "250000", "Chery": "100000", "Tesla": "999999", "BMW X5": "550000"} } A= (f["user"]) a.update ({"Food": {"Pork": "12", "Beef": "28", "Chicken": "8", "Lamb": "+",}}) f["user"]=af.close () f=shelve.open (' User _db ', ' a ') print (f["user"])
Python serialization: JSON & Pickle & Shelve Module