Solving Chinese garbled problems in Python

Source: Internet
Author: User

Garbled Reason:
Because your file is declared as Utf-8, it should also be a source file saved with Utf-8 encoding. But the local default encoding for Windows is cp936, which is GBK encoding, so the string that prints utf-8 directly in the console is, of course, garbled.

Workaround:
In the console printing place with a transcoding is OK, when printed so write:
Print Myname.decode (' UTF-8 '). Encode (' GBK ')

A more general approach would be to:
Import Sys
Type = Sys.getfilesystemencoding ()
Print Myname.decode (' UTF-8 '). Encode (type)

Article reproduced from http://hi.baidu.com/yobin/item/166e3a46537781d3c1a59257

Solving Chinese garbled problems in Python

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.