Loop-and-a-half (Re: Curious assignment behaviour)
Alex Martelli
aleax at aleax.it
Thu Oct 11 23:33:20 CEST 2001
Paul Rubin wrote:
...
>> for line in file.xreadlines():
>> ...
>
> What if you want to read until you get to a delimiter?
>
> while (line := readline()) != 'end': ...
for line in file.xreadlines():
if line == 'end': break
etcetc(line)
Of course, if you're transliterating from some other language and need to
keep close structure similarity (e.g. for didactic reasons), Python makes
that easy too, see e.g. the rather obvious approach of my recipe:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66061
which would give:
while data.set(file.readline()) != 'end':
process(data.get())
Alex
More information about the Python-list
mailing list