Skip to main content
added 97 characters in body
Source Link
Jerub
  • 42.9k
  • 15
  • 76
  • 91

The correct thing to do is use the 'string-escape' code to decode the string.

>>> myString = "spam\\neggs"
>>> decoded_string = bytes(myString, "utf-8").decode('string-escape'"unicode_escape") # python3 
>>> decoded_string = myString.decode('string_escape') # python2
>>> print(decoded_string)
spam
eggs

Don't use the AST or eval. Using the string codecs is much safer.

The correct thing to do is use the 'string-escape' code to decode the string.

>>> myString = "spam\\neggs"
>>> decoded_string = myString.decode('string-escape')
>>> print(decoded_string)
spam
eggs

Don't use the AST or eval. Using the string codecs is much safer.

The correct thing to do is use the 'string-escape' code to decode the string.

>>> myString = "spam\\neggs"
>>> decoded_string = bytes(myString, "utf-8").decode("unicode_escape") # python3 
>>> decoded_string = myString.decode('string_escape') # python2
>>> print(decoded_string)
spam
eggs

Don't use the AST or eval. Using the string codecs is much safer.

Source Link
Jerub
  • 42.9k
  • 15
  • 76
  • 91

The correct thing to do is use the 'string-escape' code to decode the string.

>>> myString = "spam\\neggs"
>>> decoded_string = myString.decode('string-escape')
>>> print(decoded_string)
spam
eggs

Don't use the AST or eval. Using the string codecs is much safer.