Omar Khalid
Omar Khalid

Reputation: 364

Converting bytes string with escape characters into a normal string

There are million questions and answers related to that but nothing worked with me.

I have a byte string that has escape characters, I want to decode it and convert it to string but without the escape characters and when printed it would be something like the following

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

Here is a part of my byte string

>>> 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

and I want this

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

Upvotes: 0

Views: 803

Answers (1)

rasjani
rasjani

Reputation: 7970

>>> print(bytes_string.decode("unicode_escape"))
Normal sentence
    Indented sentence
another one
>>>

Upvotes: 3

Related Questions