Joke Collection Website - Public benefit messages - What’s the matter with Chinese garbled characters in python?
What’s the matter with Chinese garbled characters in python?
Python reports an error invalid character in identifier, which means "invalid character in identifier". Check whether any characters are Chinese, just change the Chinese characters to English characters and then run.
The author of Python intentionally designed a very restrictive syntax so that bad programming habits (such as not indenting the next line to the right in an if statement) cannot be compiled. One of the most important ones is Python's indentation rules.
One difference from most other languages ??(such as C) is that the boundaries of a module are completely determined by the position of the first character of each line in this line (while C language uses a pair of Curly braces {} are used to clearly define module boundaries and have nothing to do with the position of characters). This has been controversial.
Because since the birth of languages ??like C, the grammatical meaning of the language has been separated from the arrangement of characters, which was once considered an improvement in programming languages. However, it is undeniable that Python does make programs clearer and more beautiful by forcing programmers to indent (including all places where modules need to be used, such as if, for, and function definitions).
Extended information
Python Chinese encoding:
The default encoding format in Python is ASCII format. Chinese characters cannot be printed correctly without modifying the encoding format, so in An error will be reported when reading Chinese.
The solution is to just add ?# -*- coding: UTF-8 -*-?or ?# coding=utf-8? at the beginning of the file
Note: # There should be no spaces on either side of the ?=? sign in coding=utf-8?.
Example:
(Python 2.)
#!/usr/bin/python
# -*- coding: UTF -8 -*-? print( "Hello, world" )
The output result is:
Hello, world
- Previous article:Subway telephone subway telephone number
- Next article:How to recharge the electronic newspaper recharge card?
- Related articles
- Which is better android push service Getui or Jiguang?
- Best wishes to my friends every morning.
- Can't find one plus 8t SMS.
- 100 10 send spam messages.
- My boyfriend sent a lot of messages in the middle of the night and all of them were withdrawn. What do you mean I watch it in the morning?
- Xi 'an: Why is temporary regional control implemented? What are the conditions for termination?
- What does DR mean? What does it mean?
- Should I go to Wuxi to get a nucleic acid test? The latest regulations on the epidemic in Wuxi
- How to obtain real-time traffic information of expressway
- Will contact with patients necessarily lead to infection in COVID-19? Is it intimate contact with the same shopping mall?