序列化和反序列化pickle和json 模組

淡然。。發表於2024-06-20

import pickle

hello = 'hello world'

data = pickle.dumps(hello) #pickle.dumps把任意物件序列化成一個bytes(位元組數)

print(data)

data1 = pickle.loads(data) #pickle.loads將位元組數反序列化

print(data1)

import json

data = {

'hello':123,

'nihao':'word'

}

str_json = json.dumps(data) #json.dumps將字典型別序列化為字串型別

print(str_json,type(str_json))

dicts = json.loads(str_json)#json.loads將字串型別反序列化為字典型別

print(dicts,type(dicts))

相關文章