python sequence of pickle and json

We objects (variables) may be stored or transmitted into the memory from the process is called serialization, pickle or json standard library with Python. After serialization, the contents can be written to disk serialized, or on another computer transmitted over a network.

pickle: Python supports all the data types (including function)

json: Support str, int, tule, list, dict

If we want to pass objects between different programming languages, it is necessary to serialize objects into a standard format, such as XML, but a better approach is serialized to json, json that out because that is a string, so the language can be read, it can be easily stored to disk or transmitted over a network. json is not only a standard format, and faster than xml, can be read directly on the web page, very convenient.

import json

#---序列化--- dic={'name':'jack','age':22,'sex':'male'} data=json.dumps(dic) f=open('json_test.txt','w') f.write(data) #--json.dump(dic.f)
f.close()
# --- --- deserialization
fr=open('json_test.txt') 
dicdata=json.loads(fr.read()) #---等价于dicdata=json.load(fr)
fz.close()
 

json: JavaScript Object Notation, is a lightweight data interchange format. Json most widely used AJAX in the web as a communications server and client data format.

Converting the object into a Python objects json two common function is the only difference between dumps and dump, the dump is converted into json Python objects generate a target file stream fp, and generates a string dumps.

Json objects will be transferred into two common Python objects and functions are load loads, the difference between supra.

 

The following is a standard library usage pickle:

import pickle

#---序列化--- dic={'name':'jack','age':22,'sex':'male'} p=pickle.dumps(dic) f=open('pickle_test','wb') #---等价于pickle.dump(dic,f) f.write(p) f.close()
#---反序列化--- fr=open('pickle_test','rb') dicata=pickle.loads(fr.read()) #---等价于dicdata=pickle.load(fr) fr.close()

  

References: https: //www.cnblogs.com/fmgao-technology/p/9078924.html 

Guess you like

Origin www.cnblogs.com/blogzyq/p/11105060.html