Basic Chinese encoding problem solving in python2.x version

Source: Internet
Author: User
Python output "Hello, world!", English no problem, but if you output Chinese characters "Hello, the World" may encounter Chinese encoding problem.
If no encoding is specified in the Python file, an error occurs during execution:

#!/usr/bin/pythonprint "Hello, World";


The above program execution output results are:

File "test.py", line 2syntaxerror:non-ascii character ' \xe4 ' in file test.py on line 2, but no encoding declared; See http://www.python.org/peps/pep-0263.html for details

The default encoding format in Python is the ASCII format, which fails to print correctly when the encoding format is not modified, so the error occurs when reading Chinese.
The workaround is to add #-*-Coding:utf-8-*-or #coding =utf-8 at the beginning of the file.
Instance (Python 2.0+)

#!/usr/bin/python#-*-coding:utf-8-*-print "Hello, World";


The output is:

Hello, world

So if you re-learn the process, the code contains Chinese, you need to specify the coding in the head.
Note: The python3.x source file uses Utf-8 encoding by default, so the Chinese can be parsed normally without specifying UTF-8 encoding.

  • 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.