[Tutor] line iteration in a file

richard kappler richkappler at gmail.com
Fri Jun 5 18:28:08 CEST 2015


SOLVED: Sometimes one just has to be an idiot. One must remember that
computers count from zero, not from one. Changes my list indexes to reflect
that small but crucial fundamental point, and all worked fine.

regards, Richard

On Wed, Jun 3, 2015 at 10:37 PM, richard kappler <richkappler at gmail.com>
wrote:

> 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.
>



-- 

Windows assumes you are an idiot…Linux demands proof.


More information about the Tutor mailing list