我們需要將記憶體中的資料進行序列化,也就是寫入檔案時,寫入的型別只能是字串或二進位型別。但是如果我們想要將複雜一些的資料類型,如:列表、字典或函數之類的同樣進行序列化,我們就要用到 json或pickle。
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'>
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'>
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
三、pickle序列化
pickle的用法和上面的相同,但是pickle序列化後的資料型別是二進位的,而pickle只能在python中是使用。
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)) #输出 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 && 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) # 写入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'}
#
以上是記憶體中資料序列化實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!