for line in file weirdness

SamIam DyRanch at gmail.com
Wed Apr 13 08:04:52 EDT 2005


I think what you need to do is to have a nested if_else statment:
for line in filelines:
        if some_condition : break
        else: do_something_else

If the if statment is excuted then break return to for_loop
else do something different then return to for_loop.
    When I read from a file I read the whole file into a variable then
work form the variable

file = open('InputString','r')                              # open file
for reading only
filelines = map(string.strip,file.readlines())      #remove newlines
for string

Then you can just use the variable filelines and loop through as much
as you like. If I can help you can email me at servando at mac.com
I also use SKYPE username servando_garcia
Hope this helped.

Cordula's Web wrote:
> Hello,
>
> here's a strange bug (?) I've came across (using Python 2.2):
>
> # loop_1
> for line in file:
>     if some_condition(line): break
>     do_something()
>
> # loop_2
> for line in file:
>     do_something_else()
>
> The problem is, that loop_2 doesn't resume where loop_1 left off, but
> skips many lines (a block's worth or so) before continuing.
>
> Why is this? Is reading from a file non-reentrant?
>
> It is always possible to slurp the whole file content into a list,
and
> then iterate through the list, but I want to handle HUGE files too.
>
> Thanks,
> -cpghost.
> 
> -- 
> Cordula's Web. http://www.cordula.ws/




More information about the Python-list mailing list