Python SyntaxError:解析时出现意外的 EOF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43189302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SyntaxError: unexpected EOF while parsing
提问by Akash Joshi
I am getting error while running this part of the code. tried some of the existing solutions, none of them helped
运行这部分代码时出现错误。尝试了一些现有的解决方案,但没有一个帮助
elec_and_weather = pd.read_csv(r'C:\HOUR.csv', parse_dates=True,index_col=0)
# Add historic DEMAND to each X vector
for i in range(0,24):
elec_and_weather[i] = np.zeros(len(elec_and_weather['DEMAND']))
elec_and_weather[i][elec_and_weather.index.hour==i] = 1
# Set number of hours prediction is in advance
n_hours_advance = 24
# Set number of historic hours used
n_hours_window = 24
for k in range(n_hours_advance,n_hours_advance+n_hours_window):
elec_and_weather['DEMAND_t-%i'% k] = np.zeros(len(elec_and_weather['DEMAND']))'
I am always getting this error
我总是收到这个错误
for i in range(0,24):
File "<ipython-input-29-db3022a769d1>", line 1
for i in range(0,24):
^
SyntaxError: unexpected EOF while parsing
File "<ipython-input-25-df0a44131c36>", line 1
for k in range(n_hours_advance,n_hours_advance+n_hours_window):
^
SyntaxError: unexpected EOF while parsing
回答by Felix
The SyntaxError: unexpected EOF while parsing
means that the end of your source code was reached before all code blocks were completed. A code block starts with a statement like for i in range(100):
and requires at least one line afterwards that contains code that should be in it.
这SyntaxError: unexpected EOF while parsing
意味着在所有代码块完成之前就到达了源代码的末尾。代码块以类似语句开头,for i in range(100):
之后至少需要一行包含应包含在其中的代码。
It seems like you were executing your program line by line in the ipython console. This works for single statements like a = 3
but not for code blocks like for loops. See the following example:
看起来您是在 ipython 控制台中逐行执行程序。这适用于单个语句a = 3
,例如 for 循环,但不适用于代码块。请参阅以下示例:
In [1]: for i in range(100):
File "<ipython-input-1-ece1e5c2587f>", line 1
for i in range(100):
^
SyntaxError: unexpected EOF while parsing
To avoid this error, you have to enter the whole code block as a single input:
为避免此错误,您必须将整个代码块作为单个输入输入:
In [2]: for i in range(5):
...: print(i, end=', ')
0, 1, 2, 3, 4,
回答by Mike G
This can simply also mean you are missing or have too many parentheses. For example this has too many, and will result in unexpected EOF
:
这也可能意味着您缺少括号或括号太多。例如这有太多,会导致意想不到的EOF
:
print(9, not (a==7 and b==6)
回答by MikeyB
My syntax error was semi-hidden in an f-string
我的语法错误半隐藏在 f 字符串中
print(f'num_flex_rows = {self.}\nFlex Rows = {flex_rows}\nMax elements = {max_elements}')
should be
应该
print(f'num_flex_rows = {self.num_rows}\nFlex Rows = {flex_rows}\nMax elements = {max_elements}')
It didn't have the PyCharm spell-check-red line under the error.
错误下没有 PyCharm 拼写检查红线。
It did give me a clue, yet when I searched on this error message, it of course did not find the error in that bit of code above.
它确实给了我一个线索,但是当我搜索此错误消息时,它当然没有在上面那段代码中找到错误。
Had I looked more closely at the error message, I would have found the '' in the error. Seeing Line 1 was discouraging and thus wasn't paying close attention :-( Searching for
如果我更仔细地查看错误消息,我会在错误中找到 ''。看到第 1 行令人沮丧,因此没有密切关注 :-( 搜索
self.)
自己。)
yielded nothing. Searching for
一无所获。正在寻找
self.
自己。
yielded practically everything :-\
几乎产生了一切:-\
If I can help you avoid even a minute longer of deskcheckingyour code, then mission accomplished :-)
如果我能帮助您避免再多花一分钟的时间检查您的代码,那么任务就完成了 :-)
C:\Python\Anaconda3\python.exe C:/Python/PycharmProjects/FlexForms/FlexForm.py File "", line 1 (self.)^ SyntaxError: unexpected EOF while parsing
Process finished with exit code 1
C:\Python\Anaconda3\python.exe C:/Python/PycharmProjects/FlexForms/FlexForm.py File "", line 1 (self.)^ SyntaxError: 解析时出现意外 EOF
进程以退出代码 1 结束
回答by Eerik Sven Puudist
Here is one of my mistakes that produced this exception: I had a try
block without any except
or finally
blocks. This will not work:
这是导致此异常的我的错误之一:我有一个try
没有任何except
或多个finally
块的块。这将不起作用:
try:
lets_do_something_beneficial()
To fix this, add an except
or finally
block:
要解决此问题,请添加一个except
orfinally
块:
try:
lets_do_something_beneficial()
finally:
lets_go_to_sleep()
回答by Prince Vijay
elec_and_weather['DEMAND_t-%i'% k] = np.zeros(len(elec_and_weather['DEMAND']))'
elec_and_weather['DEMAND_t-%i'% k] = np.zeros(len(elec_and_weather['DEMAND']))'
The error comes at the end of the line where you have the (') sign; this error always means that you have a syntax error.
错误出现在您有 (') 符号的行的末尾;这个错误总是意味着你有一个语法错误。
回答by tinyhare
There are some cases can lead to this issue, if it occered in the middle of the code it will be "IndentationError: expected an indented block" or "SyntaxError: invalid syntax", if it at the last line it may "SyntaxError: unexpected EOF while parsing":
有一些情况会导致这个问题,如果它出现在代码中间,它会是“IndentationError: expected an indented block”或“SyntaxError: invalid syntax”,如果它在最后一行它可能是“SyntaxError:unexpected解析时的EOF”:
Missing body of “if,while or for” statement-->
缺少“if,while or for”语句的主体-->
root@nest:~/workplace# cat test.py
l = [1,2,3]
for i in l:
root@nest:~/workplace# python3 test.py
File "test.py", line 3
^
SyntaxError: unexpected EOF while parsing
Unclosed parentheses (Especially in complex nested states)-->
未封闭的括号(特别是在复杂的嵌套状态中)-->
root@nest:~/workplace# cat test.py
l = [1,2,3]
print( l
root@nest:~/workplace# python3 test.py
File "test.py", line 3
^
SyntaxError: unexpected EOF while parsing