python3中string和bytes转化

来源:互联网 发布:手机淘宝店铺实名认证 编辑:程序博客网 时间:2024/05/22 06:14

创建bytes类型

a=bytes([1,2,3,4])a=bytes('hello', 'ascii')print(a)

string与bytes相互转化

  • 按uft-8形式编码

    a=’hello world’
    b=a.encode(encoding=’utf-8’)
    print(type(a), type(b))
    c=b.decode(encoding=’utf-8’)
    print(c, type(c))

  • 按gbk方式编码

    x=a.encode(encoding=’gbk’)
    print(x, type(x))
    y=x.decode()
    print(y, type(y))

原创粉丝点击