0

I have a string that looks like this:

>>> st = 'aaaaa\x12bbbbb'

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('\\\\', '\\')
falsetru
  • 357,413
  • 63
  • 732
  • 636
turtle
  • 7,533
  • 18
  • 68
  • 97

1 Answers1

4

Decode the encoded string with the same encoding:

>>> st = 'aaaaa\x12bbbbb'
>>> escaped_st = st.encode('string-escape')
>>> escaped_st
'aaaaa\\x12bbbbb'
>>> escaped_st.decode('string-escape')
'aaaaa\x12bbbbb'
falsetru
  • 357,413
  • 63
  • 732
  • 636