Python's serialization and deserialization is used for sharing between memory, including server and client sharing, sharing between two Python programs, and storing it as a string in a hard disk.
Pyhton's pickle can operate on Python's various data types, including classes, lists, objects, and so on. Pickle only applies to python.
Hard disk storage data can only be in the form of strings, which enables the interaction of memory data between two programs in this way
#Python的序列化和反序列化
Import pickle li = [' Xiaoli ', ' Xiaowang ', ' P ', ' 3 ', ' 4 ']dumped = Pickle.dumps (li) #将列表进行了序列化print dumpedloaded = Pickle.loads (dumped) #将序列化的数据进行了反序列化print loaded
stored in a file at the same time as serialized, and deserialized in a file
Import Pickleli = [' Xiaoli ', ' Xiaowang ', ' A ', ' 3 ', ' 4 ']pickle.dump (Li,open (' F:/python practice/temp.pk ', ' W ')) # After serializing the list to the file, print pickle.load (' F:/python practice/temp.pk ', ' r ') is stored #将文件中的数据读取出来进行反序列化
Use of JSON
JSON can only manipulate simple data types, and it is not possible to manipulate complex data types such as classes. JSON is a common format for various languages.
The operation of JSON is almost the same as the pickle operation
Import Jsonli = [' Xiaoli ', ' Xiaowang ', ' xiaoming ', 12]print json.dumps (li) #将列表序列化print json.loads (Json.dumps (LI)) #反序列化
Python serialization and JSON