How to convert a "raw" string into a normal string? [duplicate]
Asked Answered
P

2

32

In Python, I have a string like this:

'\\x89\\n'

How can I decode it into a normal string like:

'\x89\n'
Protist answered 16/6, 2014 at 11:16 Comment(0)
M
43

If your input value is a str string, use codecs.decode() to convert:

import codecs

codecs.decode(raw_unicode_string, 'unicode_escape')

If your input value is a bytes object, you can use the bytes.decode() method:

raw_byte_string.decode('unicode_escape')

Demo:

>>> import codecs
>>> codecs.decode('\\x89\\n', 'unicode_escape')
'\x89\n'
>>> b'\\x89\\n'.decode('unicode_escape')
'\x89\n'

Python 2 byte strings can be decoded with the 'string_escape' codec:

>>> import sys; sys.version_info[:2]
(2, 7)
>>> '\\x89\\n'.decode('string_escape')
'\x89\n'

For Unicode literals (with a u prefix, e.g. u'\\x89\\n'), use 'unicode_escape'.

Maryrose answered 16/6, 2014 at 11:24 Comment(10)
This doesn't work in python3. (str doesn't have a decode method and also string_escape isn't a valid encoding anymore).Wiedmann
@Bakuriu: The OP never specified a Python version.Maryrose
Yeah, you're right, what I was trying to decode is a bytes class object, not a str object!Protist
@user486005: that was crucial information; I had to assume you were using Python 2 (still the more likely option, statistically speaking). I've updated my answer to include the correct methods to use in Python 3.Maryrose
@MartijnPieters I didn't know that python-3 has remove the decode method of string object then, at first I thought it was a string object while it was a bytes object indeed, so it just happens to run successfully. Thanks a lot.Protist
I'm using python3. I have a variable that I get by reference. When I try to do the conversion of my variable it doesn't work. Any ideas? Example: my_string = 'C:\Program Files\Java\jre1.8.0_202\bin\java.exe' codecs.decode(my_string, 'unicode_escape') # 'C:\\Program Files\\Java\\jre1.8.0_202\x08in\\java.exe'Rousseau
@grand: you don't have any escapes in that string. Echoing a string with backslashes always shows valid, copyable Python syntax. See Why do backslashes appear twice?Maryrose
@MartijnPieters, I am trying same thing in python3. seems decode function is not available with str. how do we convert raw strings to normal strings in latest python3?Luciferous
name = "sandeep\tnagendra\n" print(name) --> this gives tab and newline print("%r" % name) --> THis converts normal string to raw string name = r"sandeep\tnagendr\n" print(name) --> This prints raw string Now how do we change raw string back to normal one?Luciferous
@sandeepnagendra please read the whole answer, there is a Python 3 section. Your first expression produces a string representation (what repr() returns), which includes quotes.Maryrose
M
12

This would work for Python 3:

b'\\x89\\n'.decode('unicode_escape')
Monovalent answered 16/6, 2014 at 11:34 Comment(1)
@user486005: I am glad it helped. It was meant as addition to the Martinj Python 2 solution.Monovalent

© 2022 - 2024 — McMap. All rights reserved.