python tutorial, how to solve the Python coding Chinese garbage problem?

Python output with "Hello, World!", English no problem, but if you output Chinese characters "Hello, world" is likely to encounter Chinese coding problem.

Python file if the encoding is not specified, an error occurs during the execution:

1  # ! / Usr / bin / python 
2  Print  " Hello, World " ;
 3   
4  '' ' 
5  What I do not know how you can add a python learning exchange buttoned qun in the learning process, 934.10917 million
 6  group, there are good tutorials, development tools and e-books.
7  Share python current business needs and your talent and how good python learning from zero base, and learn what content.
8  ''

More execution output is:

 File "test.py", line 2
SyntaxError: 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

Python in the default encoding format is an ASCII format and can not print correctly when characters did not modify the encoding format, so when reading Chinese error.

Solution to just add # at the beginning of the file - * - coding: UTF-8 - * - or the # coding = utf-8 on the line

Note: # coding = number of sides = utf-8 is not the space.

Example:

1  # ! / Usr / bin / Python 
2  # - * - Coding: UTF-8 - * - 
3   
4  Print  " Hello, world " ;

The output is:

So if everyone in the learning process, the code contains Chinese, you need to specify the encoding in the head.

Note: Python3.X source file using the utf-8 encoding by default, so you can properly resolve the Chinese, without specifying the UTF-8 encoding.

Note: If you use the editor, and the need to format storage py file is UTF-8, otherwise similar to the following error message:

1 SyntaxError: (unicode error) ‘utf-8’ codec can’t decode byte 0xc4 in position 0:
2 invalid continuation byte

Pycharm setup steps:

  • Enter the file> Settings, search encoding in the input box.
  • Found Editor> File encodings, the IDE Encoding and Project Encoding is set to utf-8.

Guess you like

Origin www.cnblogs.com/xiaoyiq/p/11141649.html