关于macos:运行Python代码中的“SyntaxError:非ASCII字符”

“SyntaxError: Non-ASCII character” in running Python code

我从python开始,当我使用解释器并运行以下代码时:

1
2
>>>peliculas = ["movie1","movie2","movie3","movie4"]
>>>print(peliculas[1])

当我使用pycharm ide时,它不会编译:

1
2
peliculas = ["movie1","movie2","movie3","movie4"]
print(peliculas[1])

这是输出:

1
2
 File"/Users/user/PycharmProjects/untitled/Prueba2.py", line 1
SyntaxError: Non-ASCII character '\xc3' in file /Users/user/PycharmProjects/untitled/Prueba2.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

Python怎么了?…我安装正确吗?


这是一个文件编码问题。尝试在文件开头添加此项:

1
#coding=utf-8


只需在python代码的顶部添加这两行。

1
2
#!/usr/bin/env python
# -*- coding: utf-8 -*-


在pycharm中,您可以通过"文件编码"菜单或状态栏上的"文件编码"项指定文件编码。请参阅此帮助文章