二、json序列化1、dumps序列化和loads反序列化dumps把数据类型转换成字符串
import json info = { 'name': 'the count of monte cristo', 'type': 'movie' } data = json.dumps(info) print(data) print(type(data)) # 输出 {"name": "the count of monte cristo", "type": "movie"} <class 'str'>
loads把字符串转换成数据类型
import json get_info = json.loads(data) print(get_info['name']) print(get_info) print(type(get_info)) #输出 the count of monte cristo {'name': 'the count of monte cristo', 'type': 'movie'} <class 'dict'>
2.dump序列化和load反序列化dump把数据类型转换成字符串并存储在文件中
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) # 第一个参数是内存中的数据对象,第二个参数是文件句柄 #写入文件中的内容 {"name": "the count of monte cristo", "type": "movie"}
load把文件打开从字符串转换成数据类型
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)) #输出 the count of monte cristo {'name': 'the count of monte cristo', 'type': 'movie'} <class 'dict'>
3.json序列化一个函数import json def test(name): print("hello,{}".format(name)) info = { 'name': 'the count of monte cristo', 'type': 'movie', 'func': test } data = json.dumps(info) #输出 file "g:/python/untitled/study6/json&pickle模块.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只能处理简单的数据类型,例如:字典、列表、字符串等,不能处理函数等复杂的数据类型。
2、json是所有语言通用的,所有语言都支持json,如果我们需要python跟其他语言进行数据交互,那么就用json格式
三、pickle序列化pickle的用法和上面的相同,但是pickle序列化后的数据类型是二进制的,并且pickle只能在python中是使用。
1.dumps && loadsimport 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)) #输出 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) #输出 hello,cat {'name': 'the count of monte cristo', 'type': 'movie', 'func': <function test at 0x00000235350a7f28>}
2. dump && loadimport 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) # 写入test.txt文件中的内容 �}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) # 输出 {'name': 'the count of monte cristo', 'func': <function test at 0x000001ba2ab4d510>, 'type': 'movie'}
总结:
json值支持简单的数据类型,pickle支持所有的数据类型。
pickle只能支持python本身的序列化和反序列化,不能用作和其他语言做数据交互,而json可以。
pickle序列化的是整个的数据对象,所以反序列化函数时,函数体中的逻辑变了,是跟着心的函数体走的。
以上就是内存中数据序列化实例的详细内容。
