[Tutor] Reading only a few specific lines of a file
Alan Gauld
alan.gauld at btinternet.com
Fri May 23 19:06:17 CEST 2008
"Marilyn Davis" <marilyn at deliberate.com> wrote
> (I'm sorry for the duplicate, Alan.)
I didn't see a duplicate! ;-)
>> linecount = 0
>> for line in file("itemconfig.txt", 'rU'):
>> if line == objectToLoad or linecount > 0:
>> if linecount == 0: linecount = 5
>> itemList.append(line.split()[:5])
>> linecount -= 1
>
> I always go for readability. So I like "for x in range(5):" to go 5
> times. I'm not sure what we should be doing 5 times either.
You could use a nested for loop but that would involve the use
of an iterator.next() function which I didn't want to confuse
the issue with, although John has pointed it out now anyway.
Since the outer for loop is doing the next() call for us it didn't
seem necessary.
> Similarly, for readability, I choose the if/elif/else form of switch
> replacement.
Are you referring to the other thread?
There is no switch here.
> Also, I don't like to see the obfuscated forms of the conditional
> operator. Heck, I'm not even crazy about the Python conditional in
> 2.5.
Similarly there is no conditional statements here? Again are
you refering to the other recent thread?
> I like to say "The point of a programming language is to communicate
> with
> other engineers (in a language that also the computer understands)."
I agree. :-)
Alan G.
More information about the Tutor
mailing list