[Tutor] about pyhton + regular expression

Jeff Shannon jeff@ccvcorp.com
Thu Mar 20 15:28:08 2003


Paul Tremblay wrote:

>On Thu, Mar 20, 2003 at 09:41:01AM -0800, Jeff Shannon wrote:
>  
>
>>for line in infile.xreadlines():
>>   [...]
>>
>
>Nifty. I had been doing:
>
>line_to_read = '1'
>while line_to_read:
>    line_to_read = infile.readline()
>  
>

Another common idiom is this:

while 1:
    line = infile.readline()
    if not line:
        break
    [...]

There's arguments back and forth about this being preferable to what 
you'd been doing (your code sets the same variable in two different 
places, which is usually considered poor design, but it's shorter and 
some people dislike 'while 1' ...) but for most purposes xreadlines() 
does exactly what's desired.

Jeff Shannon
Technician/Programmer
Credit International