python - 语法错误 : unexpected EOF while parsing

标签 python python-3.x python-3.6 eof lint

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

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): 之类的语句开头,之后至少需要一行包含应在其中的代码。

您似乎是在 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

为避免此错误,您必须将整个代码块作为单个输入输入:

In [2]: for i in range(5):
   ...:     print(i, end=', ')
0, 1, 2, 3, 4,

关于python - 语法错误 : unexpected EOF while parsing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43189302/

相关文章:

python - 机器人框架迭代列表问题

python - 从深度图计算视野

python - .logs_from() 操作的 after 属性值应该如何格式化?

python - BytesIO 对象到图像

python - 如何对每个包含两个列表的列表进行切片?

python - Pandas 更有效地过滤包含空字段的组?

python - 随机出现错误消息,我很困惑为什么会发生这种情况?

Python - 如何设置法语语言环境?

python - 过滤数据帧字符串列 - 类型 'int' 的参数不可迭代/无法使用包含 NA/NaN 值的向量进行索引

python - 计算多个列表的 95 百分位