【问题标题】:Python convert bytes to strPython将字节转换为str
【发布时间】:2021-07-21 15:12:53
【问题描述】:

如何将这个字节值:b'Ren\xc3\xa9'转换成这个字符串:'Ren\xc3\xa9'

str(b'Ren\xc3\xa9') 将返回 "b'Ren\xc3\xa9'" 这不是我需要的

我需要 str: 'Ren\xc3\xa9' 中的这个输出

【问题讨论】:

    标签: python string byte


    【解决方案1】:

    你需要解码它:

      string =  b"Ren\xc3\xa9".decode("utf-8") 
    

    【讨论】:

      【解决方案2】:
      byte_value = b'Ren/xc3/xa9'
      print(byte_value.decode("utf-8"))
      

      【讨论】:

        【解决方案3】:

        你可以得到没有前两个字符的子字符串:

        a = b'Ren\xc3\xa9'    
        str(a)[2:]
        

        【讨论】:

        • 这没有给出正确的输出,str(some_bytes_object) 给出了源代码文字的字符串表示
        • 试试str(a)[2:] == 'Ren\xc3\xa9'
        • 它将返回:"Ren\\xc3\\xa9'" 但需要 'Ren\xc3\xa9'
        猜你喜欢
        • 2015-04-16
        • 2019-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-27
        • 2022-11-07
        • 1970-01-01
        相关资源
        最近更新 更多