简体   繁体   中英

Invalid syntax in Python script

Python 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

Imported NumPy 1.7.1, SciPy 0.12.0, Matplotlib 1.3.0 + guidata 1.6.1, guiqwt 2.3.1
Type "scientific" for more details.
>>> runfile('C:/Program Files (x86)/Firaxis Games/Sid Meier's Pirates!/ModMan/MM.py', wdir=r'C:/Program Files (x86)/Firaxis Games/Sid Meier's Pirates!/ModMan')
  File "<stdin>", line 1
    runfile('C:/Program Files (x86)/Firaxis Games/Sid Meier's Pirates!/ModMan/MM.py', wdir=r'C:/Program Files (x86)/Firaxis Games/Sid Meier's Pirates!/ModMan')
                                                            ^
SyntaxError: invalid syntax
>>> 

Running Windows 7 x64/, WinPython 275, Spyder 2.7. This is run using the spyder GUI. When the ( MM.py ) copy in the python folder is run it works. BTW This is point and click I'm not typing anything.

Use " instead of ' :

runfile("C:/Program Files (x86)/Firaxis Games/Sid Meier's Pirates!/ModMan/MM.py", wdir=r"C:/Program Files (x86)/Firaxis Games/Sid Meier's Pirates!/ModMan")

Stack Overflow is nice enough to highlight your invalid syntax for you.

If some tool (spyder GUI) executes this invalid code, go get a better tool.

To actually explain the problem: Strings can be enquoted within double quotes " and single quotes ' . It does not make a difference which of these two you use, as long as the string starts and ends with the same symbol. So "Hello" is the same string as 'Hello' .

The string ends, as soon as the quote character appears for the second time. So in your case, 'Sid Meier's Pirates' , the string already ends after Meier , making the rest of it not a string but normal Python code the interpreter tries to interpret (which will usually fail).

So to use the quotation character within the string itself, you will need to escape the quotation mark. So for single quoted strings, you can use \\' and for double quoted strings \\" .

So in your case, you could write 'Sid Meier\\'s Pirates' and it would be a valid string.

The other option would be to switch the quotation characters to double quotes. That way you would not have to escape single quotation characters within the string (but double quotation characters): "Sid Meier's Pirates" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM