Reputation: 15
I want to decode a some hex in python.
In part of the string \xcd\xed\xb0\xb2
text = re.search(r'(\\x\w{2}){4}', rtf)
unicodeText = text.decode('gb2312')
Error: '_sre.SRE_Match' object has no attribute 'decode'
Hope someone can help, Thanks
Upvotes: 0
Views: 185
Reputation: 368894
re.search
returns a Match object, not a matched string.
Use group
method to get the matched string.
>>> rtf = r'\xcd\xed\xb0\xb2'
>>> matched = re.search(r'(\\x\w{2}){4}', rtf)
>>> text = matched.group()
>>> text.decode('string-escape').decode('gb2312')
u'\u665a\u5b89'
# In Python 3.x
# >>> text.encode().decode('unicode-escape').encode('latin1').decode('gb2312')
# '晚安'
BTW, you don't need to use regular expression, what you want is convert \xOO
:
Python 2.x:
>>> rtf = r'\xcd\xed\xb0\xb2'
>>> rtf.decode('string-escape').decode('gb2312')
u'\u665a\u5b89'
>>> print rtf.decode('string-escape').decode('gb2312')
晚安
Python 3.x:
>>> rtf = r'\xcd\xed\xb0\xb2'
>>> rtf.encode().decode('unicode-escape').encode('latin1').decode('gb2312')
'晚安'
Upvotes: 1