Python restores the hexadecimal-encoded string of the form "\xe4 ..." to Chinese

Source: Internet
Author: User

One of the documents processed today has been translated into the hexadecimal encoding of "\xe4 ..." and other characters are normal.

Roughly studied under the discovery of these codes three a group representing a Chinese character, because the text is mixed with normal symbols, I decided to use a regular matching method to replace three sets of hexadecimal code strings with the Chinese characters they represent.

Input file contents such as:

#\xe5\xbe\xae\xe6\x84\x9f\xe8\xb0\xa2##\xe6\x8a\xa5\xe5\x96\x9c\xef\xbc\x9a\xe4\xba\x91\xe6\xa3\x80\xe7\xb2\x89 \xe4\xb8\x9d159\xe4\xb8\x87\xe4\xba\x86#\xe5\x9c\xa8\xe8\xbf\x99\xe4\xb8\xaa\xe5\x91\xa8\xe4\xb8\x89\xe4\xb8\ X8b\xe5\x8d\x885\xe6\x97\xb6\xef\xbc\x8c\xe4\xba\x91\xe6\xa3\x80\xe5\xbe\xae\xe5\x8d\x9a\xe7\xb2\x89\xe4\xb8\ X9d\xe8\xbf\x87159\xe4\xb8\x87\xe3\x80\x82\xe8\xbf\x87\xe5\x8e\xbb\xe7\x9a\x84\xe5\xa4\x9a\xe5\xb0\x91\xe4\xb8 \xaa\xe6\x97\xa5\xe6\x97\xa5\xe5\xa4\x9c\xe5\xa4\x9c\xef\xbc\x8c\xe8\xae\xb8\xe8\xae\xb8\xe5\xa4\x9a\xe5\xa4\ x9a\xe8\xae\xa4\xe8\xaf\x86\xe7\x9a\x84\xe4\xb8\x8d\xe8\xae\xa4\xe8\xaf\x86\xe7\x9a\x84\xe6\x9c\x8b\xe5\x8f\ X8b\xe5\x92\x8c\xe5\x81\xb6\xe4\xbb\xac\xe4\xb8\x80\xe8\xb7\xaf\xe5\x90\x8c\xe8\xa1\x8c\xef\xbc\x8c\xe4\xb8\ Xba\xe4\xba\x91\xe6\xa3\x80\xe5\xbe\xae\xe5\x8d\x9a\xe5\x8f\x91\xe5\xb1\x95\xe5\x87\xba\xe5\x8a\x9b\xe7\x8c\ Xae\xe7\xad\x96\xef\xbc\x8c\xe9\xbc\x93\xe5\x8a\xb2\xe6\x89\x93\xe6\xb0\x94\xef\xbc\x8c\xe4\xb8\xba\xe4\xba\ X91\xe6\xa3\x80\xe5\xbe\xae\xe4\xbf\xa1\xe7\x82\xb9\xe8\xb5\x9e\xef\xbc\x81\xe5\x9c\xa8\xe5\x8f\x8c\xe5\xbe\xae\xe5\xb9\xb3\xe5\x8f \xb0\xe4\xb8\x8a\xef\xbc\x8c\xe5\x81\xb6\xe5\x92\x8c\xe4\xbd\xa0\xe3\x80\x81\xe5\x92\x8c\xe4\xbb\x96\xe9\x83\ Xbd\xe6\x98\xaf\xe6\x9c\x8b\xe5\x8f\x8b\xef\xbc\x8c\xe6\x98\xaf\xe6\x9c\x8b\xe5\x8f\x8b\xe6\x89\x8d\xe8\x83\ Xbd\xe5\xb9\xb3\xe7\xad\x89\xe5\xaf\xb9\xe7\xad\x89\xe4\xba\xa4\xe6\xb5\x81\xe6\xb2\x9f\xe9\x80\x9a\xe3\x80\ X82\xe5\x92\xb1\xe6\xb0\xb8\xe8\xbf\x9c\xe6\x98\xaf\xe6\x9c\x8b\xe5\x8f\x8b\xef\xbc\x8c\xe6\x84\x9f\xe8\xb0\ Xa2\xe6\x82\xa8\xef\xbc\x81

ImportRedefUnicodetostr (s): Strtobytes= []     forIinchS.split ('\\x'):        ifI! ="': Num= Int (i,16) strtobytes.append (num) a=bytes (strtobytes). Decode ()returnadefTi (m): s=Str (M.group ()) a=Unicodetostr (s)returnApat= Re.compile (r'(\\x[0-9a-fa-f][0-9a-fa-f]) {3}') with open ('filename') as file: forLineinchFile:" "For m in Re.finditer (pat,line): Print (M.group ())" "        Print(Re.sub (Pat,ti,line))

Result is

# Micro Thank you # #报喜: Cloud inspection fans 1.59 million # in this Wednesday 5 o'clock in the afternoon, cloud check Weibo fans over 1.59 million. How many days of the past, many people do not know the acquaintance of friends and we go all the way, for the cloud to check the development of micro Bo to contribute, cheer up, for the cloud inspection praise! On the double micro platform, I and you, and he are friends, is a friend to equal exchange and communication. I will always be friends, thank you! 

Python restores the hexadecimal-encoded string of the form "\xe4 ..." to Chinese

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.