Print UTF-8 files in Windows

Source: Internet
Author: User

It is estimated that this will happen during entry. I used the idle editor of python25 in Windows to run the script. I found that some Chinese characters in the result of running the script are garbled, but some of them are normal. Think twice about it. First look at the source file encoding format, is the UTF-8. After searching, searching, debugging, debugging, and several compilers, it is worse than idle (encode setting may be required ). The problem was finally solved. It took me nearly five hours to write it here. I hope that people who encounter the problem can search for it and will not commit another mistake.


cause of garbled characters:

because your file is declared as UTF-8, it should also be a source file stored in UTF-8 encoding. However, the local encoding for Windows is cp936 by default, that is, GBK encoding. Therefore, the local code is displayed on the console.

Of course, it is garbled to print UTF-8 strings directly.


Solution:

It is okay to use a transcoding tool in the printing area on the console. It is written as follows during printing:

Print myname. Decode ('utf-8'). encode ('gbk ')


The general method should be:

Import sys

Type = SYS. getfilesystemencoding ()

Print myname. Decode ('utf-8'). encode (type)

 

Http://note.sdo.com/u/1445822006/n/mbnUS ~ Jrqk7mlx00e000xb

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.