Huang Cong: python is developed by pycharm. The program output on the python console contains garbled characters and Chinese characters.

Source: Internet
Author: User
  1. File encoding is UTF-8 while adding #-*-coding: UTF-8 -*-
  2. All strings in the file are prefixed with U, such as U "Chinese"
  3. When you input (capture some UTF-8/GBK webpages) and output (output to webpages and terminals), remember that python is stored in Unicode.
  4. Decode and Unicode are required for all input, encode is used to convert the output to the desired encoding (output in MacOS X/Linux is UTF-8, and output in Windows is GBK, however, if it is in the pycharm console, the output in Windows is UTF-8 and can be displayed normally)
  5. There are many other ancient solutions, such as using GBK in a unified manner. For example, using UTF-8 instead of Unicode for intermediate encoding.
  6. The source code file uses coding: UTF-8 to specify the encoding and save the file as UTF-8.
  7. To avoid unnecessary errors when executed as the _ main _ module, use the \ n line feed.
  8. If you need to embed Chinese characters and other non-ASCII characters in the source code, we recommend that you use the U prefix as a unicode string so that no encoding conversion can be performed during print,
  9. Otherwise, if the console encoding is inconsistent with the source file encoding, explicit conversion is required. The system language and encoding can be obtained through locale. getdefalocallocale ().
  10. It is best to convert the file path to SYS. getfilesystemencoding () encoding.
  11. Unicode strings must be converted to a certain Byte encoding when writing files.
  12. JSON supports UTF-8 encoding by default.
  13. Use UTF-8 and Unicode whenever possible, except for the file path and write files mentioned earlier.
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.