Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting \x escaped string to UTF-8 [duplicate]

How can convert a string that looks like '\xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82' to something readable with either Perl or Python?

like image 812
synapse Avatar asked Aug 08 '13 06:08

synapse


1 Answers

In python 2.7

>>> print '\xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82'
привет

>>> print '\\xd0\\xbf\\xd1\\x80\\xd0\\xb8\\xd0\\xb2\\xd0\\xb5\\xd1\\x82'.decode('string-escape')
привет
>>> print r'\xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82'.decode('string-escape')
привет

In python 3.x

>>> br'\xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82'.decode('unicode-escape').encode('latin1').decode('utf-8')
'привет'
like image 85
falsetru Avatar answered Oct 05 '22 23:10

falsetru