python -> json and pickle module

A, json and pickle module

1.1 Serialization

The objects (variables) may be stored or transmitted into the memory from the process is called serialization, called pickling in Python, in other languages ​​also called serialization, marshalling, flattening.

Serialization advantages:

  1. Persist status: Memory is not permanently stored data, when the program runs for some time, we power down or restart the program, in-memory data on the program for some time before the (structured) have been cleared. But before power off or restart the program that are currently all the data in memory are saved (saved to a file) to perform data can be loaded from the file before the program to the next, and then continue, which is serialized .
  2. Cross-platform data exchange: not only the contents of the serialized write serialization disk, you can also transmitted over the network to another machine, if the two sides agreed to send and receive good practical one sequence of the format, then it broke platform / language difference limit brought achieve a cross-platform data exchange.

1.2 json

json module:

  1. Wrote a program in python, using java to write a program, both programs need to exchange data between the provisions of a common data type in multiple languages
  2. json string:
    1. Serialization: (most common) becomes python json string from the dictionary, dump
    2. Deserialize: from json into python string dictionary (the most common), load

Json serialization is not unique to python, json serialization in java and other languages ​​will be involved, so use json serialization can achieve the purpose of cross-platform data transmission.

json data types and data type correspondence table python

Json type Python type
{} dict
[] list
"string" str
520.13 int or float
true/false True/False
null None

json module serialization and deserialization of a process as shown in FIG.

json module

# 序列化
with open('Json序列化对象.json', 'w') as fw:
    json.dump(struct_data, fw)
# 反序列化
with open('Json序列化对象.json') as fr:
    data = json.load(fr)
print(data)
{'name': 'json', 'age': 23, 'sex': 'male'}
import json
struct_data = {'name': 'json', 'age': 23, 'sex': 'male'}
print(struct_data, type(struct_data))

#
{'name': 'json', 'age': 23, 'sex': 'male'} <class 'dict'>
data = json.dumps(struct_data)
print(data, type(data))
#
{"name": "json", "age": 23, "sex": "male"} <class 'str'>
# 注意:无论数据是怎样创建的,只要满足json格式(如果是字典,则字典内元素都是双引号),就可以json.loads出来,不一定非要dumps的数据才能loads
data = json.loads(data)
print(data, type(data))
#
{'name': 'json', 'age': 23, 'sex': 'male'} <class 'dict'>

1.3 pickle

Pickle serialization and unique serialization problem with all other programming languages, it can only be used Python, and may not be compatible with each other different versions of Python, therefore, can only be saved unimportant data Pickle, that is not successful deserialize it does not matter. But the benefits of pickle that can store all data types in Python, including objects, json can not.

pickle module serialization and deserialization process shown below

pickle module

# 序列化(注意:pickle模块需要使用二进制存储,即'wb'模式存储)
with open('Pickle序列化对象.pkl', 'wb') as fw:
    pickle.dump(struct_data, fw)
# 反序列化
with open('Pickle序列化对象.pkl', 'rb') as fr:
    pickle = pickle.load(fr)
print(data)
#
{'name': 'json', 'age': 23, 'sex': 'male'}
import pickle
struct_data = {'name': 'json', 'age': 23, 'sex': 'male'}
print(struct_data, type(struct_data))
#
{'name': 'json', 'age': 23, 'sex': 'male'} <class 'dict'>
data = pickle.dumps(struct_data)
print(data, type(data))
#
b'\x80\x03}q\x00(X\x04\x00\x00\x00nameq\x01X\x04\x00\x00\x00jsonq\x02X\x03\x00\x00\x00ageq\x03K\x17X\x03\x00\x00\x00sexq\x04X\x04\x00\x00\x00maleq\x05u.' <class 'bytes'>
data = pickle.loads(data)
print(data, type(data))
#
{'name': 'json', 'age': 23, 'sex': 'male'} <class 'dict'>

Guess you like

Origin www.cnblogs.com/SkyOceanchen/p/11402594.html