为什么ipython repper告诉我“语法:解析时出乎意料的eof”当我输入代码时?
在运行代码的这一部分时,我会遇到错误。我尝试了一些现有的解决方案,但它们都没有帮助。
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']))'
我总是会遇到此错误:
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
indle或命令行替补中相关问题: command-line输入syntaxerror 子>
I'm getting an error while running this part of the code. I tried some of the existing solutions, but 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 always get 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
Related problem in IDLE or the command-line REPL: Command-line input causes SyntaxError
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我下面有同样的错误:
,当我忘记尾随的括号时,如下所示:
或者,当我忘记将
Pass
放入类或功能时,如下所示:I got the same error below:
When I forgot a trailing parenthesis as shown below:
Or, when I forgot to put
pass
to class or function as shown below:有时,由于忘记添加try/except语句以外的添加。
Sometimes its due to forgetting to add an Except on a try/except statement.
有些情况可能会导致此问题,如果它出现在代码中间,它将是“ InsentationError:期望的缩进块”或“ SyntaxeRror:Invalid语法”,如果它在最后一行,则可能是Syntaxerror:意外eof解析“:
丢失“ if”,“ while”和“ for”的陈述的尸体 - &gt;
未封闭的括号(尤其是在复杂的嵌套状态下) - &gt;
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":
Missing the body of "if","while"and"for" statement-->
Unclosed parentheses (Especially in complex nested states)-->
尝试
eval
一个空字符串时,我遇到了此错误。例如:
我改用
json.loads()
而不是错误。I encountered this error while trying to
eval
an empty string.for example:
I used
json.loads()
instead and the error was gone.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.
这是我产生此例外的我的错误之一:我有一个
尝试
block,而没有任何或或
最后
blocks。这将行不通:要解决此问题,添加
除外
或最后
block:Here is one of my mistakes that produced this exception: I had a
try
block without anyexcept
orfinally
blocks. This will not work:To fix this, add an
except
orfinally
block:这也可能意味着您失踪或括号太多。例如,这太多了,并且会导致意外的
eof
: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
:我的语法错误是在F弦中半隐藏的,
应该是
在错误下没有Pycharm Spell-Check-Red线。
它确实给了我一个线索,但是当我搜索此错误消息时,它当然没有在上面的代码中找到错误。
如果我更仔细地查看了错误消息,我会在错误中找到“”。看到第1行是令人沮丧的,因此没有引起密切关注:-(搜索
什么都没有。搜索
几乎可以产生一切: - \
如果我可以帮助您避免使用 deckecking 您的代码更长的一分钟,然后完成任务:-)
My syntax error was semi-hidden in an f-string
should be
It didn't have the PyCharm spell-check-red line under the error.
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
yielded nothing. Searching for
yielded practically everything :-\
If I can help you avoid even a minute longer of deskchecking your code, then mission accomplished :-)
SyntaxError:解析时出乎意料的EOF
意味着在完成所有代码块之前已达到源代码的结尾。一个代码块以在范围内的i之类的语句开始:
,之后至少需要一行包含在其中的代码。看来您是在Ipython控制台中逐线执行程序。这适用于
a = 3
之类的单个语句,但对于loops等代码块而无效。请参阅以下示例:要避免此错误,您必须将整个代码块作为单个输入输入:
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 likefor i in range(100):
and requires at least one line afterwards that contains code that should be in it.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:To avoid this error, you have to enter the whole code block as a single input: