import json
d = {'name':'Tom', 'age':20, 'interest':('music', 'move'), 'class':['python']}
j = json.dumps(d)
print(j, type(j))
{"name": "Tom", "age": 20, "interest": ["music", "move"], "class": ["python"]}
d1 = json.loads(j)
print(d1)
{'name': 'Tom', 'age': 20, 'interest': ['music', 'move'], 'class': ['python']}
import pickle
d = {'name':'Tom', 'age':20, 'interest':('music', 'move'), 'class':['python']}
p = pickle.dumps(d)
print(p, type(p), len(p))
b'\x80\x03}q\x00(X\x04\x00\x00\x00nameq\x01X\x03\x00\x00\x00Tomq\x02X\x03\x00\x00\x00ageq\x03K\x14X\x08\x00\x00\x00interestq\x04X\x05\x00\x00\x00musicq\x05X\x04\x00\x00\x00moveq\x06\x86q\x07X\x05\x00\x00\x00classq\x08]q\tX\x06\x00\x00\x00pythonq\nau.' 111
d2 = pickle.loads(p)
print(d2, type(d2), len(d2))
{'name': 'Tom', 'age': 20, 'interest': ('music', 'move'), 'class': ['python']} 4
在PyCharm终端中敲出以下命令
pip install msgpack
输入后会有进度条显示,成功后会有提示,然后就可以导入使用了
import msgpack
d = {'name':'Tom', 'age':20, 'interest':('music', 'move'), 'class':['python']}
data = msgpack.dumps(d)
print(data, type(data), len(data))
b'\x84\xa4name\xa3Tom\xa3age\x14\xa8interest\x92\xa5music\xa4move\xa5class\x91\xa6python' 50
d1 = msgpack.unpackb(data)
print(d1, type(d1), len(d1))
{b'name': b'Tom', b'age': 20, b'interest': [b'music', b'move'], b'class': [b'python']} 4
d2 = msgpack.loads(data)
print(d2, type(d2), len(d2))
{b'name': b'Tom', b'age': 20, b'interest': [b'music', b'move'], b'class': [b'python']} 4