In Python, serialization can be understood as: converting Python's object code into json string, and anti serialization can be interpreted as decoding the json format string into Python data object. In the standard library of python, json library and pickle library are provided to deal with this part.
Two modules for serialization
json: Conversion between strings and Python data types
pickle: Used for conversion between Python-specific types and python data types
json provides four functions: dumps,dump,loads,load
pickle provides four functions: dumps,dump,loads,load
#json.dumps() transforms a Python data structure to JSON
#json.loads() converts a JSON encoded string to a Python data structure.
data1={'name':'orange','age':22} print(type(data1),data1) #Dictionary serialization into json format strings data2=json.dumps(data1,ensure_ascii=False)#If there is Chinese, we need to add parameter processing. print(type(data2),data2) #String deserialization in json format into dictionary data3=json.loads(data2) print(type(data3),data3) <class 'dict'> {'name': 'orange', 'age': 22} <class 'str'> {"name": "orange", "age": 22} <class 'dict'> {'name': 'orange', 'age': 22}
# pickle.dumps() converts data into strings only known in python through special formats
# pickle.loads() converts pickle data into python data structure
data4={'name':'Litchi','age':22} print(type(data4),data4) # dumps converts data into strings that are only known in python through special formats data5=pickle.dumps(data4) print(type(data5),data5) # loads convert pickle data into python data structure data6=pickle.loads(data5) print(type(data6),data6) <class 'dict'> {'name': 'Litchi', 'age': 22} <class 'bytes'> b'\x80\x03}q\x00(X\x04\x00\x00\x00nameq\x01X\x06\x00\x00\x00\xe8\x8d\x94\xe6\x9e\x9dq\x02X\x03\x00\x00\x00ageq\x03K\x16u.' <class 'dict'> {'name': 'Litchi', 'age': 22}
# Serialization and deserialization of json (dump,load) file content
list1=['selenium','appium','android','ios','uiautomator'] #json.dump() is used to convert dict-type data into str and write it to the txt/json file. with open(r'C:\Users\wangli\PycharmProjects\Test\Test\1.txt','w') as f: json.dump(list1,f) #json.load() is used to read data from txt/json files with open(r'C:\Users\wangli\PycharmProjects\Test\Test\1.txt','r') as f: print(json.load(f)) ['selenium', 'appium', 'android', 'ios', 'uiautomator']
# Serialization and deserialization of pickle(dump,load) file content
list2=['selenium','appium','android','ios'] # dump converts data into strings only recognized by python language in a special form and writes them to files with open(r'C:\Users\wangli\PycharmProjects\Test\Test\2.txt','wb') as f: pickle.dump(list2,f) # load reads data from a data file and converts it to python's data structure with open(r'C:\Users\wangli\PycharmProjects\Test\Test\2.txt','rb') as f: print(pickle.load(f)) ['selenium', 'appium', 'android', 'ios']
Usage of eval ()
list1='[1,2,3]' list2=eval(list1) list3=str(list2) print(type(list1),list1,type(list2),list2,type(list3),list3) tuple1='(1,2,3)' tuple2=eval(tuple1) tuple3=str(tuple2) print(type(tuple1),tuple1,type(tuple2),tuple2,type(tuple3),tuple3) dict1="{'name':'orange','age':22}" dict2=eval(dict1) dict3=str(dict2) print(type(dict1),dict1,type(dict2),dict2,type(dict3),dict3) <class 'str'> [1,2,3] <class 'list'> [1, 2, 3] <class 'str'> [1, 2, 3] <class 'str'> (1,2,3) <class 'tuple'> (1, 2, 3) <class 'str'> (1, 2, 3) <class 'str'> {'name':'orange','age':22} <class 'dict'> {'name': 'orange', 'age': 22} <class 'str'> {'name': 'orange', 'age': 22} Process finished with exit code 0