- 字节和字符串之间的转换
# bytes object
b = b"example"
# str object
s = "example"
# str to bytes
bytes(s, encoding = "utf8")
# bytes to str
str(b, encoding = "utf-8")
# an alternative method
# str to bytes
str.encode(s)
# bytes to str
bytes.decode(b)
- 字典—字符串
使用这个 转换表 将 obj 序列化为 JSON 格式的 str。 其参数的含义与 dump() 中的相同。
转码表:
示例
d = {'type': 'dict'}
s = json.dumps(d)
print(s,type(s))
响应
{"type": "dict"} <class 'str'>
- 字符串,字节,字节串—字典
使用这个 转换表 将 s (一个包含 JSON 文档的 str, bytes 或 bytearray 实例) 反序列化为 Python 对象
转码表:
示例
s = '{"type": "string"}'
obj = json.loads(s)
print(obj,type(obj))
响应
{'type': 'string'} <class 'dict'>