python3 类型转换(str,bytes,dict)

  • 字节和字符串之间的转换
 # 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'>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值