Convert escaped string back to original string
Question:
I have a string that looks like this:
>>> st = 'aaaaax12bbbbb'
I can convert it to a raw string via:
>>> escaped_st = st.encode('string-escape')
'aaaaa\x12bbbbb'
How can I convert the escaped string back to the original string? I was trying to do something like this:
escaped_st.replace('\\', '\')
Answers:
Decode the encoded string with the same encoding:
>>> st = 'aaaaax12bbbbb'
>>> escaped_st = st.encode('string-escape')
>>> escaped_st
'aaaaa\x12bbbbb'
>>> escaped_st.decode('string-escape')
'aaaaax12bbbbb'
I have a string that looks like this:
>>> st = 'aaaaax12bbbbb'
I can convert it to a raw string via:
>>> escaped_st = st.encode('string-escape')
'aaaaa\x12bbbbb'
How can I convert the escaped string back to the original string? I was trying to do something like this:
escaped_st.replace('\\', '\')
Decode the encoded string with the same encoding:
>>> st = 'aaaaax12bbbbb'
>>> escaped_st = st.encode('string-escape')
>>> escaped_st
'aaaaa\x12bbbbb'
>>> escaped_st.decode('string-escape')
'aaaaax12bbbbb'