简体   繁体   English

python:无法实例化一个或两个以上变量(引发SyntaxError)

[英]python : cannot instantiate more than one or two variables (SyntaxError raised)

Since yesterday, I'm having a strange issue when testing new Python scripts. 从昨天开始,在测试新的Python脚本时我遇到了一个奇怪的问题。 When running the script, I get a 'SyntaxError: invalid syntax' on one of the first variable assignment. 运行脚本时,在第一个变量分配之一上出现“ SyntaxError:无效语法”。

For exemple, this simple code I made for loading screen options from a text file (that exists in the same directory) : 例如,我编写了以下简单代码,用于从文本文件(位于同一目录中)加载屏幕选项:

#!/usr/bin/env python2.7

import ConfigParser

config = ConfigParser.RawConfigParser()
config.read('config.txt')
fullscreen = bool(config.get('Screen', 'fullscreen')
width = int(config.get('Screen', 'width')
height = int(config.get('Screen', 'height')
...

When run in the terminal, returns : 在终端中运行时,返回:

File "./config.py", line 8
width = int(config.get('Screen', 'width')
    ^
SyntaxError: invalid syntax

More strangely, when I comment the fullscreen line out, I get the same error... but on the next line (height = ...). 更奇怪的是,当我注释掉全屏显示行时,我得到了相同的错误...但是在下一行(高度= ...)。 Meaning the last one was indeed syntax valid? 意思是最后一个确实在语法上有效吗? Also the problem only occurs on the new files, older ones work perfectly (well, at least there's no error :) ). 同样,问题仅出现在新文件上,旧文件可以正常工作(嗯,至少没有错误:))。 And, last but not least... when I'm using another text editor (like kate, I usually use vim), writing the exact same code raises no error either 最后但并非最不重要的一点……当我使用另一个文本编辑器(如kate时,我通常使用vim)时,编写完全相同的代码也不会引起错误

I tried to reinstall vim as I thought that was where the problem came from, but it's still the same. 我试图重新安装vim,因为我以为这是问题的根源,但还是一样。

Thanks for your time and sorry for my english, Charles 感谢您的时间,对不起我的英语,查尔斯

You're missing a trailing right bracket to close bool : 您缺少右方括号来关闭bool

fullscreen = bool(config.get('Screen', 'fullscreen'))
                                                    ^

And the same with int : int相同:

width = int(config.get('Screen', 'width'))
                                         ^
height = int(config.get('Screen', 'height'))
                                           ^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python如果N个变量中有多个是真的 - Python if more than one of N variables is true 如何在文本 PYTHON 中插入两个以上的变量 - how to insert more than two variables into text PYTHON 在python中将多个变量传递给os.system - passing more than one variables to os.system in python 从 Python 中的多个“.env”文件中读取环境变量 - Reading environment variables from more than one ".env" file in Python Python:用一个以上的分隔符将字符串分成两列 - Python: Split string into two columns by more than one seperator Python SyntaxError: cannot assign to operator - 将变量定义为 for 循环中的字符串总和 - Python SyntaxError: cannot assign to operator - Defining variables as sum of strings in for loop 在 Python 中将变量实例化为 None - Instantiate variables to None in Python 为什么在这里引发IndentationError而不是SyntaxError? - Why is an IndentationError being raised here rather than a SyntaxError? 如何将多个python变量传递到正则表达式中,以便从文本文件中查找行? - How to pass more than one python variables into regex insearch of finding lines from a text file? Python Pandas:如何过滤存储在不同变量中的多个表达式的数据帧? - Python Pandas: How to filter a dataframe with more than one expression stored in different variables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM