Skip to main content
Rollback to Revision 2
Source Link
Fred Nurk
  • 14.3k
  • 4
  • 40
  • 63
>>> escaped_str = 'One \\\'example\\\'' >>> print escaped_str.encode('string_escape') One \\\'example\\\' >>> print escaped_str.decode('string_escape') One 'example'  >>> escaped_str = 'One \\\'example\\\'' >>> print escaped_str.encode('string_escape') One \\\'example\\\' >>> print escaped_str.decode('string_escape') One 'example' 

Several similar codecs are available, such as rot13 and hex.

The above is Python 2.x, but – since you said (below, in a comment) that you're using Python 3.x – while it's circumlocutious to decode a Unicode string object, it's still possible. The codec has been renamed to "unicode_escape" too:

>>> escaped_str = "One \\\'example\\\'" >>> import codecs >>> print(codecs.getdecoder("unicode_escape")(escaped_str)[0]) One 'example'  Python 3.3a0 (default:b6aafb20e5f5, Jul 29 2011, 05:34:11) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> escaped_str = "One \\\'example\\\'" >>> import codecs >>> print(codecs.getdecoder("unicode_escape")(escaped_str)[0]) One 'example' 
>>> escaped_str = 'One \\\'example\\\'' >>> print escaped_str.encode('string_escape') One \\\'example\\\' >>> print escaped_str.decode('string_escape') One 'example' 

Several similar codecs are available, such as rot13 and hex.

The above is Python 2.x, but – since you said (below, in a comment) that you're using Python 3.x – while it's circumlocutious to decode a Unicode string object, it's still possible. The codec has been renamed to "unicode_escape" too:

>>> escaped_str = "One \\\'example\\\'" >>> import codecs >>> print(codecs.getdecoder("unicode_escape")(escaped_str)[0]) One 'example' 
 >>> escaped_str = 'One \\\'example\\\'' >>> print escaped_str.encode('string_escape') One \\\'example\\\' >>> print escaped_str.decode('string_escape') One 'example' 

Several similar codecs are available, such as rot13 and hex.

The above is Python 2.x, but – since you said (below, in a comment) that you're using Python 3.x – while it's circumlocutious to decode a Unicode string object, it's still possible. The codec has been renamed to "unicode_escape" too:

 Python 3.3a0 (default:b6aafb20e5f5, Jul 29 2011, 05:34:11) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> escaped_str = "One \\\'example\\\'" >>> import codecs >>> print(codecs.getdecoder("unicode_escape")(escaped_str)[0]) One 'example' 
Removed python interpreter header, changed <pre> to four spaces (allowing automatic syntax highlighting)
Source Link
Denilson Sá Maia
  • 49.9k
  • 35
  • 115
  • 115
 >>> escaped_str = 'One \\\'example\\\'' >>> print escaped_str.encode('string_escape') One \\\'example\\\' >>> print escaped_str.decode('string_escape') One 'example' >>> escaped_str = 'One \\\'example\\\'' >>> print escaped_str.encode('string_escape') One \\\'example\\\' >>> print escaped_str.decode('string_escape') One 'example' 

Several similar codecs are available, such as rot13 and hex.

The above is Python 2.x, but – since you said (below, in a comment) that you're using Python 3.x – while it's circumlocutious to decode a Unicode string object, it's still possible. The codec has been renamed to "unicode_escape" too:

 Python 3.3a0 (default:b6aafb20e5f5, Jul 29 2011, 05:34:11) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> escaped_str = "One \\\'example\\\'" >>> import codecs >>> print(codecs.getdecoder("unicode_escape")(escaped_str)[0]) One 'example' >>> escaped_str = "One \\\'example\\\'" >>> import codecs >>> print(codecs.getdecoder("unicode_escape")(escaped_str)[0]) One 'example' 
 >>> escaped_str = 'One \\\'example\\\'' >>> print escaped_str.encode('string_escape') One \\\'example\\\' >>> print escaped_str.decode('string_escape') One 'example' 

Several similar codecs are available, such as rot13 and hex.

The above is Python 2.x, but – since you said (below, in a comment) that you're using Python 3.x – while it's circumlocutious to decode a Unicode string object, it's still possible. The codec has been renamed to "unicode_escape" too:

 Python 3.3a0 (default:b6aafb20e5f5, Jul 29 2011, 05:34:11) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> escaped_str = "One \\\'example\\\'" >>> import codecs >>> print(codecs.getdecoder("unicode_escape")(escaped_str)[0]) One 'example' 
>>> escaped_str = 'One \\\'example\\\'' >>> print escaped_str.encode('string_escape') One \\\'example\\\' >>> print escaped_str.decode('string_escape') One 'example' 

Several similar codecs are available, such as rot13 and hex.

The above is Python 2.x, but – since you said (below, in a comment) that you're using Python 3.x – while it's circumlocutious to decode a Unicode string object, it's still possible. The codec has been renamed to "unicode_escape" too:

>>> escaped_str = "One \\\'example\\\'" >>> import codecs >>> print(codecs.getdecoder("unicode_escape")(escaped_str)[0]) One 'example' 
added 616 characters in body
Source Link
Fred Nurk
  • 14.3k
  • 4
  • 40
  • 63
 >>> escaped_str = 'One \\\'example\\\'' >>> print escaped_str.encode('string_escape') One \\\'example\\\' >>> print escaped_str.decode('string_escape') One 'example' 

Several similar codecs are available, such as rot13 and hex.

The above is Python 2.x, but – since you said (below, in a comment) that you're using Python 3.x – while it's circumlocutious to decode a Unicode string object, it's still possible. The codec has been renamed to "unicode_escape" too:

 Python 3.3a0 (default:b6aafb20e5f5, Jul 29 2011, 05:34:11) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> escaped_str = "One \\\'example\\\'" >>> import codecs >>> print(codecs.getdecoder("unicode_escape")(escaped_str)[0]) One 'example' 
 >>> escaped_str = 'One \\\'example\\\'' >>> print escaped_str.encode('string_escape') One \\\'example\\\' >>> print escaped_str.decode('string_escape') One 'example' 

Several similar codecs are available, such as rot13 and hex.

 >>> escaped_str = 'One \\\'example\\\'' >>> print escaped_str.encode('string_escape') One \\\'example\\\' >>> print escaped_str.decode('string_escape') One 'example' 

Several similar codecs are available, such as rot13 and hex.

The above is Python 2.x, but – since you said (below, in a comment) that you're using Python 3.x – while it's circumlocutious to decode a Unicode string object, it's still possible. The codec has been renamed to "unicode_escape" too:

 Python 3.3a0 (default:b6aafb20e5f5, Jul 29 2011, 05:34:11) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> escaped_str = "One \\\'example\\\'" >>> import codecs >>> print(codecs.getdecoder("unicode_escape")(escaped_str)[0]) One 'example' 
Source Link
Fred Nurk
  • 14.3k
  • 4
  • 40
  • 63
Loading