Python Chinese string Processing implementation code _python
Source: Internet
Author: User
>>> teststr = ' My eclipse can't decode GBK code correctly! '
>>> Teststr
' \xe6\x88\x91\xe7\x9a\x84eclipse\xe4\xb8\x8d\xe8\x83\xbd\xe6\xad\xa3\xe7\xa1\xae\xe7\x9a\x84\xe8\xa7\xa3\xe7\ Xa0\x81gbk\xe7\xa0\x81\xef\xbc\x81 '
>>> tests2 = U ' My eclipse can't decode GBK code correctly! '
>>> test3 = Tests2.encode (' gb2312 ')
>>> test3
' \xce\xd2\xb5\xc4eclipse\xb2\xbb\xc4\xdc\xd5\xfd\xc8\xb7\xb5\xc4\xbd\xe2\xc2\xebgbk\xc2\xeb\xa3\xa1 '
>>> test3
' \xce\xd2\xb5\xc4eclipse\xb2\xbb\xc4\xdc\xd5\xfd\xc8\xb7\xb5\xc4\xbd\xe2\xc2\xebgbk\xc2\xeb\xa3\xa1 '
>>> Teststr
' \xe6\x88\x91\xe7\x9a\x84eclipse\xe4\xb8\x8d\xe8\x83\xbd\xe6\xad\xa3\xe7\xa1\xae\xe7\x9a\x84\xe8\xa7\xa3\xe7\ Xa0\x81gbk\xe7\xa0\x81\xef\xbc\x81 '
>>> test3.decode (' gb2312 '). Encode (' Utf-8 ')
' \xe6\x88\x91\xe7\x9a\x84eclipse\xe4\xb8\x8d\xe8\x83\xbd\xe6\xad\xa3\xe7\xa1\xae\xe7\x9a\x84\xe8\xa7\xa3\xe7\ Xa0\x81gbk\xe7\xa0\x81\xef\xbc\x81 '
>>> test3.decode (' gb2312 '). Encode (' utf-8 ') = = Teststr
True
As seen above, the TEST3 variable (gb2312 encoding) is decoded (into a Unicode string) and then used Utf-8 encoding, which is the same string as the Teststr value.
We also found from the example above that the Unicode string is a bridge between the gb2312 string (which Windows uses in this format) and the Utf-8 string (used by Python itself).
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.