dumps => 卸出廊敌,從內(nèi)存中導出弄砍,把數(shù)據(jù)從內(nèi)存中變成可存儲或可傳輸?shù)睦杪蛄谢?=> 將Python數(shù)據(jù)轉(zhuǎn)換為另一種格式的數(shù)據(jù)
loads => 加載凛剥,將數(shù)據(jù)加載至內(nèi)存,把序列化后的數(shù)據(jù)重新讀到內(nèi)存里悯姊,反序列化 => 將其他格式的數(shù)據(jù)轉(zhuǎn)換為Python數(shù)據(jù)
pickle和json模塊
序列化和反序列化是對同一數(shù)據(jù)在不同格式間的來回轉(zhuǎn)換吠架。
python提供的模塊 pickle
和json
可以實現(xiàn)序列化和反序列化锋喜。
pickle
0. 導入模塊
import pickle
1. pickle - dumps - 序列化
pickle.dumps()
方法能把任意數(shù)據(jù)對象序列化成一個bytes
格式凡涩,之后可以寫入文件中
pickle.dumps(數(shù)據(jù)對象)
示例:
>>> import pickle
>>> d = dict(name='Bob', age=20, score=88)
>>> pickle.dumps(d)
b'\x80\x03}q\x00(X\x03\x00\x00\x00ageq\x01K\x14X\x05\x00\x00\x00scoreq\x02KXX\x04\x00\x00\x00nameq\x03X\x03\x00\x00\x00Bobq\x04u.'
2.pickle - dump - 序列化
pickle.dump()
能夠直接把任意數(shù)據(jù)對象序列化后寫入file-like object:
示例:
# 把數(shù)據(jù)d序列化后寫入文件dump.txt中
>>> f = open('dump.txt', 'wb')
>>> pickle.dump(d, f)
>>> f.close()
3. pickle - loads - 反序列化
pickle.loads()
方法能把pickle.dumps()序列化后的數(shù)據(jù)反序列化回來
>>> d = b'\x80\x03}q\x00(X\x03\x00\x00\x00ageq\x01K\x14X\x05\x00\x00\x00scoreq\x02KXX\x04\x00\x00\x00nameq\x03X\x03\x00\x00\x00Bobq\x04u.'
>>> pickle.dumps(d)
{'name'='Bob', 'age'=20, 'score'=88}
4. pickle - load - 反序列化
pickle.load()
方法能直接把pickle.dumps()序列化后保存在文件中的數(shù)據(jù)反序列化回來
>>> f = open('dump.txt', 'rb')
>>> d = pickle.load(f) # 能夠直接反序列化文件中的序列化數(shù)據(jù)
>>> f.close()
>>> d
{'age': 20, 'score': 88, 'name': 'Bob'}
json
0. 導入模塊
import json
1. json - dumps - 序列化為json字符串
json.dumps()
方法能夠把Python數(shù)據(jù)序列化為 json字符串 :
import json
d = {"name": jane, "score": 88}
b = json.dumps(d)
# 返回json字符串
>>> b = '{"name": jane, "score": 88}'
2. json - dump - 直接寫入json字符串至文件
json.dump()
方法可以直接把序列化后的JSON字符串寫入一個file-like Object
:
import json
d = {"name": jane, "score": 88}
f = open('json.txt', 'w')
json.dump(d, f) # 將d序列化成json字符串后棒搜,寫入json.txt
f.close()
3. json - loads - 將json字符串反序列化
json.loads()
方法可以將一個json字符串數(shù)據(jù)反序列化成為Python標準數(shù)據(jù):
import json
b = '{"name": jane, "score": 88}'
d = json.loads(b)
# 返回python類型數(shù)據(jù)
>>> d = {"name": jane, "score": 88}
4. json - load - 從文件中讀取json字符串
json.load()
方法可以直接從一個寫有json字符串的文件中讀出json字符串并反序列化為python數(shù)據(jù):
import json
f = open('json.txt', 'r')
d = json.load(f) # 直接讀取f中的json字符串并反序列化
>>> d = {"name": jane, "score": 88}
Json進階 - 序列化類的實例對象為json字符串
-
將類對象進行序列化,轉(zhuǎn)換為json字符串:
import json
class Student(object):
def __init__(self, name, score):
self.name = name
self.score = score
# 將Student實例轉(zhuǎn)換為dict對象函數(shù)
def student2dict(stu):
return {
'name': stu.name,
'score': stu.score
}
stu = Student('David', 88)
d = json.dumps(stu, default=student2dict)
過程:student2dict函數(shù)先將stu對象轉(zhuǎn)換為python字典 => json.dumps()將python字典序列化為json字符串
-
將json字典活箕,反序列化為類的實例對象
import json
class Student(object):
def __init__(self, name, score):
self.name = name
self.score = score
# 將Student實例轉(zhuǎn)換為dict對象函數(shù)
def dict2student(stu_dict):
return Student(stu_dict['name'], stu_dict['score'])
stu_json = '{"name": "Bob", "score": 98}'
d = json.loads(stu_json, object_hook=dict2student)
過程:json.loads()先將stu_json反序列化為python字典 => dict2student函數(shù)根據(jù)python字典實例化對象