V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
smallpython
V2EX  ›  Python

在 python3 中, 字节流如何转换成字符串

  •  
  •   smallpython · 2020-08-03 10:46:28 +08:00 · 3802 次点击
    这是一个创建于 1333 天前的主题,其中的信息可能已经有所发展或是发生改变。

    环境: python3

    假如我创建这样一个二进制的字节流

    b = b'\xaa\xbb\xcc'

    那么我怎样才能得到字符串

    "aabbcc"

    13 条回复    2020-08-04 14:33:47 +08:00
    knightdf
        1
    knightdf  
       2020-08-03 10:51:38 +08:00
    b.decode(xxx)
    si
        2
    si  
       2020-08-03 10:52:24 +08:00
    你这个意思是要字符串转十六进制吗,那直接搜索就有了
    CharAct3
        3
    CharAct3  
       2020-08-03 10:55:22 +08:00   ❤️ 2
    b.hex()
    smallpython
        4
    smallpython  
    OP
       2020-08-03 11:06:27 +08:00
    @CharAct3 感谢
    smallpython
        5
    smallpython  
    OP
       2020-08-03 11:09:15 +08:00
    @si 是十六进制转字符串吧, 用字符串的形式表现一个字节流的具体内容
    hakono
        6
    hakono  
       2020-08-03 14:40:48 +08:00
    1 楼都直接给出正确答案了。。。。。
    fasionchan
        7
    fasionchan  
       2020-08-03 15:02:45 +08:00
    1 楼说的就是答案,推荐看一些关于 bytes 对象、str 对象以及文本编码的材料:

    文本编码: https://python.fasionchan.com/zh_CN/latest/practices/coding.html
    str 对象: https://mp.weixin.qq.com/s/E7z2yN8V9ac2pUTfgqDg9Q
    ytymf
        8
    ytymf  
       2020-08-03 16:12:58 +08:00
    明明 3 楼才是正确答案,都不好好看看楼主问题。
    imn1
        9
    imn1  
       2020-08-03 16:26:16 +08:00
    #3 才是正解,看需求不是类型转换
    decode 得不到"aabbcc"
    wittyfans
        10
    wittyfans  
       2020-08-03 20:09:30 +08:00 via iPhone
    记的一点笔记,希望有所帮助:

    # string to bytes, then to numbers
    s = '你好'
    list(bytes(s,encoding='utf8'))
    Output:[228, 189, 160, 229, 165, 189]

    # string to bytes then to string
    bytes(s,encoding='utf8').decode('utf8')
    Output:'你好'

    # list numbers to bytes then to string
    bytes([228, 189, 160, 229, 165, 189]).decode('utf8')
    Output:'你好'

    # str
    s = 'hello'

    # bytes
    b = b'hello'

    # ok
    s + s

    # error
    try:
    s + b
    except TypeError:
    print('发生错误')
    Output:发生错误
    lolizeppelin
        11
    lolizeppelin  
       2020-08-04 13:50:20 +08:00
    你先说清楚要干嘛
    内部直接 decode
    网络传输直接 struct pack unpack
    ctype 就转 ctype 的对应的
    smallpython
        12
    smallpython  
    OP
       2020-08-04 14:30:07 +08:00
    @lolizeppelin 就是如题目所说呀, 三楼就是我要的答案, 他说之前我都不知道还有这个方法....
    smallpython
        13
    smallpython  
    OP
       2020-08-04 14:33:47 +08:00
    @wittyfans 谢谢你的答案, 但是你这个想表达的是先转换成数字, 然后再把数字转换成 16 进制字符串吗, 感觉绕了一圈
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2998 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 14:47 · PVG 22:47 · LAX 07:47 · JFK 10:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.