python中文编码

Python 中文编码

Python 输出 "Hello, World!",英文没有问题,但是如果你输出中文字符"你好,世界"就有可能会碰到中文编码问题。

Python 文件中如果未指定编码,在执行过程会出现报错:



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



Python中默认的编码格式是 ASCII 格式,在没修改编码格式时无法正确打印汉字,所以在读取中文时会报错。

解决方法为只要在文件开头加入 # -*- coding: UTF-8 -*- 或者 #coding=utf-8 就行了



Pycharm 设置步骤:

  • 进入 file > Settings,在输入框搜索 encoding
  • 找到 Editor > File encodings,将 IDE Encoding 和 Project Encoding 设置为utf-8。

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

猜你喜欢

转载自blog.csdn.net/qq_32792879/article/details/79158805