Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a "raw" string into a normal string?

Tags:

In Python, I have a string like this:

'\\x89\\n' 

How can I decode it into a normal string like:

'\x89\n' 
like image 407
Wenlin.Wu Avatar asked Jun 16 '14 11:06

Wenlin.Wu


1 Answers

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'.

like image 180
Martijn Pieters Avatar answered Oct 20 '22 20:10

Martijn Pieters