python 序列化

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 序列化相关的知识,希望对你有一定的参考价值。

import json
from io import BytesIO
from rest_framework.parsers import JSONParser
from rest_framework.renderers import JSONRenderer


dic_data = {‘id‘: 2, ‘name‘: ‘Metis Finance‘}
print(dic_data)
print(type(dic_data))

# dict - > str
json_data = json.dumps(dic_data)
print(json_data)
print(type(json_data))

# str - > byte
b_data = bytes(json_data,encoding=‘utf8‘)
print(b_data)
print(type(b_data))

# byte - > dict
dict_data2 = JSONParser().parse(BytesIO(b_data))
print(dict_data2)
print(type(dict_data2))

# dic - > byte
b_data2 = JSONRenderer().render(dic_data)
print(b_data2)
print(type(b_data2))

  

{‘id‘: 2, ‘name‘: ‘Metis Finance‘}
<class ‘dict‘>
{"id": 2, "name": "Metis Finance"}
<class ‘str‘>
b‘{"id": 2, "name": "Metis Finance"}‘
<class ‘bytes‘>
{‘id‘: 2, ‘name‘: ‘Metis Finance‘}
<class ‘dict‘>
b‘{"id":2,"name":"Metis Finance"}‘
<class ‘bytes‘>

Process finished with exit code 0

  

以上是关于python 序列化的主要内容,如果未能解决你的问题,请参考以下文章

python 有用的Python代码片段

Python 向 Postman 请求代码片段

python [代码片段]一些有趣的代码#sort

使用 Python 代码片段编写 LaTeX 文档

python 机器学习有用的代码片段

python 代码片段和解决方案