0
votes

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.

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

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

If I change the script to:

print(“what invalid char in identifier!?”);

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.

2
Your quotes aren't straight " quotes. Are you writing this is TextEdit? - mhlester

2 Answers

3
votes

You have smart quotes!

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 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.

3
votes

You are using quotes that are not equivalent to the quotes used in python to delineate strings --

" vs “

>>> '“' == '"'
False

Replace them and the error will go away.