python脚本无效字符 [英] python script invalid character

查看:160
本文介绍了python脚本无效字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Mac OS X 10.9.2上使用python运行脚本,但没有遇到这个无效字符问题.

I'm trying to run scripts using python on a mac osx 10.9.2, and I'm not getting this invalid character problem I'm having.

我有一个只有这一行的脚本: 打印为什么这不起作用?什么是非ASCII字符?";

I have a script with just this line: print "why does this not work? what non-ascii char?";

如果我使用python(2.7.5)运行它,则会出现此错误:

If I run it using python (2.7.5), it gives this error:

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

如果我将脚本更改为:

print("what invalid char in identifier!?");

并使用python 3.4.0运行它,我得到:

and run it with python 3.4.0, I get:

File "script.3.py", line 1
    print("what invalid char in identifier!?");
          ^
SyntaxError: invalid character in identifier

有人可以帮忙吗?如何指定字符编码?谢谢.

Can someone please help? How do I specify char coding? Thanks.

推荐答案

您有引号!

也许您将源代码输入到文字处理器中,或者从自动格式错误的网站上复制了源代码.不管哪种方式,只要用直引号("字符)替换它们即可.

Perhaps you typed your source code into a word processor, or copied it from a website with bad auto-formatting. Either way, just replace them with straight quotes (the " character).

Python 2抱怨智能引号不是ASCII的一部分. Python 3可以使用非ASCII字符,但是抱怨它们不是期望的引号.无论哪种方式,解决方案都是相同的.

Python 2 complains that the smart quotes are not part of ASCII. Python 3 is okay with non-ASCII characters but complains that they aren't the expected kind of quotes. Either way, the solution is the same.

这篇关于python脚本无效字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆