Python Forum
beginner : unexpected EOF errors
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
beginner : unexpected EOF errors
#1
Hi..
I am new to python programming. I am trying to generate a raster plot here.

my code:
from brian2 import *
import matplotlib


start_scope()
cnt=1
ct=0
f= open("list.txt")
dict={}
i=[]
t=[]
lines = f.readlines()
for l in lines :
    if l[0]==0 : 
        ct=ct+1 
        b = ct
    
    elif (l[0]!= 0): 
        ind =l
      
    if ind not in dict: 
        dict[ind]=cnt
    t.append(b)
    i.append(dict[ind])
    cnt=cnt+1

plot(t,i,'.k')
xlabel('Time (ms)')
ylabel('Neuron index')
When I execute it i dont get any errors. But i dont get the desired output also. The IF statement inside the FOR loop was not executed. When i tried to debug the program using pdb, I get this.

(Pdb) from brian2 import *
(Pdb) import matplotlib
(Pdb) 
(Pdb) 
(Pdb) start_scope()
(Pdb) cnt=1
(Pdb) ct=0
(Pdb) f= open("list.txt")
(Pdb) dict={}
(Pdb) i=[]
(Pdb) t=[]
(Pdb) lines = f.readlines()
(Pdb) for l in lines :
*** SyntaxError: unexpected EOF while parsing
(Pdb)     if l[0]==0 : 
*** SyntaxError: unexpected EOF while parsing
(Pdb)         ct=ct+1 
(Pdb)         b = ct
*** The specified object '= ct' is not a function or was not found along sys.path.
(Pdb)     
*** The specified object '= ct' is not a function or was not found along sys.path.
(Pdb)     elif (l[0]!= 0): 
*** SyntaxError: invalid syntax
(Pdb)         ind =l
*** NameError: name 'l' is not defined
(Pdb)       
*** NameError: name 'l' is not defined
(Pdb)     if ind not in dict: 
*** SyntaxError: unexpected EOF while parsing
(Pdb)         dict[ind]=cnt
*** NameError: name 'ind' is not defined
(Pdb)     t.append(b)
*** NameError: name 'b' is not defined
(Pdb)     i.append(dict[ind])
*** NameError: name 'ind' is not defined
(Pdb)     cnt=cnt+1
(Pdb) 
(Pdb) plot(t,i,'.k')
[<matplotlib.lines.Line2D object at 0x7fd91dfab0b8>]
(Pdb) xlabel('Time (ms)')
Text(0.5,0,'Time (ms)')
(Pdb) ylabel('Neuron index')
Text(0,0.5,'Neuron index')
PLEASE HELP TO DEBUG THIS

MY INPUT IS A FILE. FILE CONTENT
0001
2000
2004
3000
3004
0002
2001
2010
3010
3011
0003
2020
3020
3031
Reply
#2
first of all, don't use dict as a variable name, it's a built-in function and using it as a variable name you overwrite it.
second, when you read from file, it will read string, so l[0]!= 0 will be true ('0' != 0). Probably that is why you don't get your expected result
Reply
#3
(Mar-19-2018, 02:34 PM)buran Wrote: first of all, don't use dict as a variable name, it's a built-in function and using it as a variable name you overwrite it. second, when you read from file, it will read string, so l[0]!= 0 will be true ('0' != 0). Probably that is why you don't get your expected result
Thank you for your suggestion. Can you please tell me how can i otherwise check whether the 0th element is 0 or not?
Reply
#4
you need to check it against str

l[0]!= '0':
    # do something
else:
    # do something ese
or

if l[0].startswith('0'):
    # do something if it start with 0
else:
    # do something else
note that your code has other problems too - e.g. there could be case when b is not defined on line #23 to name one...
Reply
#5
(Mar-19-2018, 04:00 PM)buran Wrote: you need to check it against str
l[0]!= '0': # do something else: # do something ese
or
if l[0].startswith('0'): # do something if it start with 0 else: # do something else
note that your code has other problems too - e.g. there could be case when b is not defined on line #23 to name one...
Thank you so much again... yea.. it does have that "name error". I have shared the pdb version also- shows other errors like "SyntaxError: unexpected EOF while parsing" . what should i do to rectify it?
Reply
#6
forget about pdb for now... resolve problems that raise when you run it.
Reply
#7
(Mar-19-2018, 04:15 PM)buran Wrote: forget about pdb for now... resolve problems that raise when you run it.
Thank you so much for your help. The code runs without any error and I get the desired output also. :D
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  getting unexpected indent errors trying to move cells up jensengt 4 882 Jun-28-2023, 12:05 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020