2018-11-01
阅读量:
1242
Python3中bytes类型转换为str类型
基于URL解析报文的时候,URL链接出现str类型+bytes类型是报错TypeError,如何解决这个问题呢?
解决方案
方法一:利用str函数
方法二:利用bytes.decode()进行解码
测试代码如下:
# -*- coding: utf-8 -*-"""Created on Wed Sep 2610:13:072018
任务:Python3 bytes类型转换为str类型
"""b1 =b'Hello's1 ='Hello'print(type(b1))print(type(s1))# bytes类型转换为str类型# 方法1 str()函数s2 =str(b1, encoding="utf-8")print(s2)print(type(s2))# 方法2 bytes.decode()函数s3 =bytes.decode(b1)print(s3)print(type(s3))
测试结果如下:

0.0000
0
4
关注作者
收藏
评论(0)
发表评论
暂无数据
推荐帖子
0条评论
0条评论
0条评论

