1. Purpose
You need to serialize the data in memory, that is, when you write to a file, you can only write to a string or binary type.But if we want to serialize more complex data types like lists, dictionaries, or functions, we use json or pickle.
2. json serialization
1. dumps serialization and loads deserialization
dumps converts data types to strings
import json info = { 'name': 'The Count of Monte Cristo', 'type': 'Movie' } data = json.dumps(info) print(data) print(type(data)) # output {"name": "The Count of Monte Cristo", "type": "Movie"} <class 'str'>
loads converts strings to data types
import json get_info = json.loads(data) print(get_info['name']) print(get_info) print(type(get_info)) #output The Count of Monte Cristo {'name': 'The Count of Monte Cristo', 'type': 'Movie'} <class 'dict'>
2.dump serialization and load deserialization
dump converts data types to strings and stores them in files
import json info = { 'name': 'The Count of Monte Cristo', 'type': 'Movie' } with open("test.txt", "w", encoding="utf-8") as f: json.dump(info, f) # The first parameter is an in-memory data object, and the second parameter is a file handle #Write content to file {"name": "The Count of Monte Cristo", "type": "Movie"}
load converts a file open from a string to a data type
import json with open("test.txt", "r", encoding="utf-8") as f: data_from_file = json.load(f) print(data_from_file['name']) print(data_from_file) print(type(data_from_file)) #output The Count of Monte Cristo {'name': 'The Count of Monte Cristo', 'type': 'Movie'} <class 'dict'>
3.json serializes a function
import json def test(name): print("hello,{}".format(name)) info = { 'name': 'The Count of Monte Cristo', 'type': 'Movie', 'func': test } data = json.dumps(info) #output File "G:/python/untitled/study6/json&pickle Modular.py", line 22, in <module> data = json.dumps(info) File "G:\python\install\lib\json\__init__.py", line 230, in dumps return _default_encoder.encode(obj) File "G:\python\install\lib\json\encoder.py", line 198, in encode chunks = self.iterencode(o, _one_shot=True) File "G:\python\install\lib\json\encoder.py", line 256, in iterencode return _iterencode(o, 0) File "G:\python\install\lib\json\encoder.py", line 179, in default raise TypeError(repr(o) + " is not JSON serializable") TypeError: <function test at 0x0000021B13C57F28> is not JSON serializable
1. json can only handle simple data types, such as dictionaries, lists, strings, etc. it cannot handle complex data types such as functions.
2. JSON is common to all languages and all languages support json. If we need python to interact with data in other languages, use JSON format
3. Pile serialization
The use of a pickle is the same as above, but the serialized data type of the pickle is binary and the pickle can only be used in python.
1.dumps && loads
import pickle def test(name): print("hello,{}".format(name)) info = { 'name': 'The Count of Monte Cristo', 'type': 'Movie', 'func': test } data = pickle.dumps(info) print(data) print(type(data)) #output b'\x80\x03}q\x00(X\x04\x00\x00\x00nameq\x01X\x19\x00\x00\x00The Count of Monte Cristoq\x02X\x04\x00\x00\x00typeq\x03X\x05\x00\x00\x00Movieq\x04X\x04\x00\x00\x00funcq\x05c__main__\ntest\nq\x06u.' <class 'bytes'>
import pickle get_data = pickle.loads(data) get_data['func']('cat') print(get_data) #output hello,cat {'name': 'The Count of Monte Cristo', 'type': 'Movie', 'func': <function test at 0x00000235350A7F28>}
2. dump && load
import pickle def test(name): print("hello,{}".format(name)) info = { 'name': 'The Count of Monte Cristo', 'type': 'Movie', 'func': test } with open('test.txt', 'wb') as f: pickle.dump(info, f) # Write to the contents of the test.txt file �}q (X typeqX MovieqX funcqc__main__ test qX nameqX The Count of Monte Cristoqu.
import pickle with open('test.txt', 'rb') as f: get_data = pickle.load(f) print(get_data) # output {'name': 'The Count of Monte Cristo', 'func': <function test at 0x000001BA2AB4D510>, 'type': 'Movie'}
Summary:
- The json value supports simple data types, and the pickle supports all data types.
- pickle can only support python's own serialization and deserialization, not for data interaction with other languages, but json can.
- Pile serializes the entire data object, so when you deserialize a function, the logic in the body of the function changes, following the body of the heart.