[Tutor] line iteration in a file

richard kappler richkappler at gmail.com
Thu Jun 4 04:37:18 CEST 2015


Figured out the string delimiters problem, thanks for all the help. Now
I've run into another.

I've used the re.finditer that I think it was Peter suggested. So I have:

for line in file:
            s = line
            t = [m.start() for m in re.finditer(r"]", s)]
            q = len(t)

which works fine, in testing it finds the number and position of the ]'s in
any line I throw at it. I then wrote a series of if/elif statements based
on q, in other words

if q == 1:
    do something
elif q == 2:
    do something else
elif q == 3:
    do a third thing
else:
    pass

as I looked through enough example to figure out that the most ]'s I can
have is 3, but the pass is there just in case.

I keep getting a list index out of range error, and my best guess is that
it's because t and q are set on the first line read, not each line read, is
that right? If not, what might be the problem and either way, how do I fix
it?

regards, Richard
who is proving to his Linux box that he is an idiot pretty regularly
-- 

Windows assumes you are an idiot…Linux demands proof.


More information about the Tutor mailing list