Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
2 views
in Python by (47.6k points)

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

3 Answers

0 votes
by (106k points)

To get rid of this error you can simply also mean you are missing or have too many parentheses. For example, this has too many, and will result in unexpected EOF:

print(10, not (a==8 and b==7)

+1 vote
by (20.3k points)

SyntaxError: unexpected EOF while parsing means that the end of your source code was reached before all code blocks were completed. 

The code block starts with a statement like for i in range(100): and requires at least one line afterward that contains code that should be in it.

It looks like you were executing the program line by line in the ipython console. This will work for single statements like a = 3 but not for code blocks like for loops. 

Have a look at 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

And to avoid this error, you should enter the whole code block as a single input like this:

In [2]: for i in range(5):

   ...:     print(i, end=', ')

0, 1, 2, 3, 4,

0 votes
by (160 points)

This error can happen even when you don't give an except block after indenting everything in the try block. That was the problem for me.

try:

if mdes == "yes" or mdes == "YES" or mdes == "Yes" or mdes == "yEs" or mdes == "yeS" or mdes == "YEs" or mdes == "yES":

    start()

elif mdes == 'no' or mdes == 'No' or mdes == 'nO' or mdes == 'NO':

    quit

This is what happened to me. If my answer is helpful, vote up! Thanks

Related questions

0 votes
0 answers
0 votes
1 answer
asked Aug 24, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...