语法错误:解析时出现意外的 EOF [英] SyntaxError: unexpected EOF while parsing

查看:38
本文介绍了语法错误:解析时出现意外的 EOF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行这部分代码时遇到错误.我尝试了一些现有的解决方案,但都没有帮助.

I am 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']))'

我总是收到这个错误:

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

推荐答案

SyntaxError:unexpected EOF while parsing 意味着在所有代码块完成之前就到达了源代码的末尾.代码块以类似于 for i in range(100): 的语句开头,并且后面至少需要一行包含应该在其中的代码.

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.

您似乎是在 ipython 控制台中逐行执行程序.这适用于像 a = 3 这样的单个语句,但不适用于像 for 循环这样的代码块.请参见以下示例:

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:

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,

这篇关于语法错误:解析时出现意外的 EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆