Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing = with '\x' and then decoding in python

I fetched the subject of an email message using python modules and received string

'=D8=B3=D9=84=D8=A7=D9=85_=DA=A9=D8=AC=D8=A7=D8=A6=DB=8C?=' 

I know the string is encoded in 'utf-8'. Python has a method called on strings to decode such strings. But to use the method I needed to replace = sign with \x string. By manual interchange and then printing the decoded resulting string, I get the string سلام_کجائی which is exactly what I want. The question is how I can do the interchange automatically? The answer seems harder than just simple usage of functions on strings like replace function.

Below I brought the code I used after manual operation?

r='\xD8\xB3\xD9\x84\xD8\xA7\xD9\x85_\xDA\xA9\xD8\xAC\xD8\xA7\xD8\xA6\xDB\x8C'
print r.decode('utf-8')

I would appreciate any workable idea.

like image 641
alexander Avatar asked Mar 24 '13 22:03

alexander


1 Answers

Just decode it from quoted-printable to get utf8-encoded bytestring:

In [35]: s = '=D8=B3=D9=84=D8=A7=D9=85_=DA=A9=D8=AC=D8=A7=D8=A6=DB=8C?='
In [36]: s.decode('quoted-printable')
Out[36]: '\xd8\xb3\xd9\x84\xd8\xa7\xd9\x85_\xda\xa9\xd8\xac\xd8\xa7\xd8\xa6\xdb\x8c?'

Then, if needed, from utf-8 to unicode:

In [37]: s.decode('quoted-printable').decode('utf8')
Out[37]: u'\u0633\u0644\u0627\u0645_\u06a9\u062c\u0627\u0626\u06cc?'

 

In [39]: print s.decode('quoted-printable')
سلام_کجائی?
like image 86
Pavel Anossov Avatar answered Oct 02 '22 16:10

Pavel Anossov