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
This question is related to
python
python-3.x
python-3.6
eof
lint
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.
My syntax error was semi-hidden in an f-string
print(f'num_flex_rows = {self.}\nFlex Rows = {flex_rows}\nMax elements = {max_elements}')
should be
print(f'num_flex_rows = {self.num_rows}\nFlex Rows = {flex_rows}\nMax elements = {max_elements}')
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
self.)
yielded nothing. Searching for
self.
yielded practically everything :-\
If I can help you avoid even a minute longer of deskchecking your code, then mission accomplished :-)
C:\Python\Anaconda3\python.exe C:/Python/PycharmProjects/FlexForms/FlexForm.py File "", line 1 (self.) ^ SyntaxError: unexpected EOF while parsing
Process finished with exit code 1
You're missing a closing parenthesis )
in print()
:
print('{0}+{1}={2}'.format(n1,n2,t1))
and you're also not storing the returned value from int()
, so z
is still a string.
z = input('?')
z = int(z)
or simply:
z = int(input('?'))
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-->
root@nest:~/workplace# cat test.py
l = [1,2,3]
for i in l:
root@nest:~/workplace# python3 test.py
File "test.py", line 3
^
SyntaxError: unexpected EOF while parsing
Unclosed parentheses (Especially in complex nested states)-->
root@nest:~/workplace# cat test.py
l = [1,2,3]
print( l
root@nest:~/workplace# python3 test.py
File "test.py", line 3
^
SyntaxError: unexpected EOF while parsing
Here is one of my mistakes that produced this exception: I had a try
block without any except
or finally
blocks. This will not work:
try:
lets_do_something_beneficial()
To fix this, add an except
or finally
block:
try:
lets_do_something_beneficial()
finally:
lets_go_to_sleep()
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
:
print(9, not (a==7 and b==6)
Maybe this is what you mean to do:
import random
x = 0
z = input('Please Enter an integer: ')
z = int(z) # you need to capture the result of the expressioin: int(z) and assign it backk to z
def main():
for i in range(x,z):
n1 = random.randrange(1,3)
n2 = random.randrange(1,3)
t1 = n1+n2
print('{0}+{1}={2}'.format(n1,n2,t1))
main()
Here's a link on the range() function: http://docs.python.org/release/1.5.1p1/tut/range.html
Source: Stackoverflow.com