SyntaxError:解析时出现意外的 EOF

2022-01-31 00:00:00 python python-3.x python-3.6 eof lint

问题描述

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

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,

相关文章