【问题标题】:Converting bytes string with escape characters into a normal string将带有转义字符的字节字符串转换为普通字符串
【发布时间】:2021-09-08 10:42:16
【问题描述】:

与此相关的问题和答案数以百万计,但对我没有任何帮助。

我有一个包含转义字符的字节字符串,我想对其进行解码并将其转换为字符串,但没有转义字符,打印时将类似于以下内容

>>> normal_string = "Hello\r\nWorld"
>>> print(normal_string)
Hello
World

这是我的字节串的一部分

>>> bytes_string = b'Normal sentence\r\n\tIndented sentence\r\nanother one'
>>> converted_string = str(bytes_string, 'utf-8')
>>> print(converted_string)
Normal sentence\r\n\tIndented sentence\r\nanother one

我想要这个

>>> print(string_that_I_want)
Normal sentence
    Indented sentence
another one

【问题讨论】:

  • 我得到了你想要的输出。

标签: python python-3.x string character-encoding


【解决方案1】:
>>> print(bytes_string.decode("unicode_escape"))
Normal sentence
    Indented sentence
another one
>>>

【讨论】:

    猜你喜欢
    • 2016-06-18
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    • 2016-09-30
    • 2019-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多