Re: [Q] How to ignore the first line of the text read from a file

On Thu, 28 Aug 2008 10:16:45 -0700, norseman wrote:

> Benjamin Kaplan wrote:
>> On Thu, Aug 28, 2008 at 12:11 AM, youngjin.michael <at> gmail.com <
>> youngjin.michael <at> gmail.com> wrote:
>> 
>>> Hello,
>>>
>>> I am new to Python and have one simple question to which I cannot find
>>> a satisfactory solution.
>>> I want to read text line-by-line from a text file, but want to ignore
>>> only the first line. I know how to do it in Java (Java has been my
>>> primary language for the last couple of years) and following is what I
>>> have in Python, but I don't like it and want to learn the better way
>>> of doing it.
>>>
>>> file = open(fileName, 'r')
>>> lineNumber = 0
>>> for line in file:
>>>    if lineNumber == 0:
>>>        lineNumber = lineNumber + 1
>>>    else:
>>>        lineNumber = lineNumber + 1
>>>        print line
>>>
>>> Can anyone show me the better of doing this kind of task?
>>>
>>> Thanks in advance.
>>>
>>> --
(Continue reading)

Dennis Lee Bieber | 29 Aug 00:11

Re: [Q] How to ignore the first line of the text read from a file

On 28 Aug 2008 19:32:45 GMT, Marc 'BlackJack' Rintsch <bj_666 <at> gmx.net>
declaimed the following in comp.lang.python:

> On Thu, 28 Aug 2008 10:16:45 -0700, norseman wrote:
> > import os
> > 
> > file = open(filename, 'r')
> > for line in file:
> >    dummy=line
> >    for line in file:
> >      print line
> > 
> > 
> > is cleaner and faster.
> 
> That's not cleaner, that's a 'WTF?'!  A ``for`` line over `file` that 
> does *not* iterate over the file but is just there to skip the first line 
> and a completely useless `dummy` name.  That's seriously ugly and 
> confusing.
>
	Nice to see someone else was as, uhm, offended by that code sample
as I was -- I just lacked the vocabulary to put it across cleanly, so
didn't respond.

	Yes, the "dummy" statement could be completely dropped to the same
effect -- still leaving the useless outer loop...

	I suspect I'd probably just have written

fin = open(filename, "r")	#fin -> File INput
(Continue reading)

Steven D'Aprano | 29 Aug 02:00

Re: [Q] How to ignore the first line of the text read from a file

On Thu, 28 Aug 2008 15:11:39 -0700, Dennis Lee Bieber wrote:

> On 28 Aug 2008 19:32:45 GMT, Marc 'BlackJack' Rintsch <bj_666 <at> gmx.net>
> declaimed the following in comp.lang.python:
> 
>> On Thu, 28 Aug 2008 10:16:45 -0700, norseman wrote:
>> > import os
>> > 
>> > file = open(filename, 'r')
>> > for line in file:
>> >    dummy=line
>> >    for line in file:
>> >      print line
>> > 
>> > 
>> > is cleaner and faster.
>> 
>> That's not cleaner, that's a 'WTF?'!  A ``for`` line over `file` that
>> does *not* iterate over the file but is just there to skip the first
>> line and a completely useless `dummy` name.  That's seriously ugly and
>> confusing.
>>
> 	Nice to see someone else was as, uhm, offended by that code sample
> as I was -- I just lacked the vocabulary to put it across cleanly, so
> didn't respond.
> 
> 	Yes, the "dummy" statement could be completely dropped to the same
> effect -- still leaving the useless outer loop...

Nevertheless, I've just done some timeit tests on the two code snippets, 
(Continue reading)


Gmane