error detail:
% python today.py
File "today.py", line 2
SyntaxError: Non-ASCII character '\xe4' in file today.py on line 2, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
solutions:
take UTF-8 as an example.
First:
#!/usr/bin/python
#coding:utf-8
from datetime import datetime
print(datetime.now().strftime('今天是%Y年%m月%d日'))
Second:
#!/usr/bin/python
#-*-coding:utf-8 -*-
from datetime import datetime
print(datetime.now().strftime('今天是%Y年%m月%d日'))
Third:
#!/usr/bin/python
#vim: set fileencoding:utf-8
from datetime import datetime
print(datetime.now().strftime('今天是%Y年%m月%d日'))
Note: Python will default to ASCII as standard encoding if no other encoding hints are given.
To define a source code encoding, a magic comment must be placed into the source files either as first or second line in the file
references:
1. https://www.python.org/dev/peps/pep-0263/
2. https://www.fatalerrors.org/a/syntaxerror-non-ascii-character-but-no-encoding-declared.html
当Python文件中出现非ASCII字符且未声明编码时,会抛出SyntaxError。为解决此问题,可以将源代码设置为UTF-8编码。方法包括:1. 在文件开头添加魔法注释作为编码声明;2. 使用符合UTF-8的文本编辑器保存文件;3. 确保编辑器配置正确。Python默认标准编码为ASCII,除非另有声明。
6335

被折叠的 条评论
为什么被折叠?



