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

This is a discussion on [Q] How to ignore the first line of the text read from a file within the Python forums in Programming Languages category; 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 ...

Go Back   Application Development Forum > Programming Languages > Python

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-28-2008, 12:11 AM
youngjin.michael@gmail.com
Guest
 
Default [Q] How to ignore the first line of the text read from a file

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.

Reply With Quote
  #2  
Old 08-28-2008, 01:12 AM
Marc 'BlackJack' Rintsch
Guest
 
Default Re: [Q] How to ignore the first line of the text read from a file

On Wed, 27 Aug 2008 21:11:26 -0700, youngjin.michael@gmail.com wrote:

> 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?


input_file = open(filename)
lines = iter(input_file)
lines.next() # Skip line.
for line in lines:
print line
input_file.close()

Ciao,
Marc 'BlackJack' Rintsch
Reply With Quote
  #3  
Old 08-28-2008, 03:36 AM
Chris
Guest
 
Default Re: How to ignore the first line of the text read from a file

On Aug 28, 6:11*am, "youngjin.mich...@gmail.com"
<youngjin.mich...@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.


fileInput = open(filename, 'r')
for lnNum, line in enumerate(fileInput):
if not lnNum:
continue
print line
Reply With Quote
  #4  
Old 08-28-2008, 03:47 AM
Santiago Romero
Guest
 
Default Re: How to ignore the first line of the text read from a file

> 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.


Why don't you read and discard the first line before processing the
rest of the file?

file = open(filename, 'r')
file.readline()
for line in file: print line,

(It works).
Reply With Quote
  #5  
Old 08-28-2008, 05:53 AM
Ken Starks
Guest
 
Default Re: [Q] How to ignore the first line of the text read from a file

youngjin.michael@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.
>


LineList=open(filename,'r').readlines()[1,]
for line in Linelist:
blah blah
Reply With Quote
  #6  
Old 08-28-2008, 06:01 AM
Paul Rubin
Guest
 
Default Re: [Q] How to ignore the first line of the text read from a file

Ken Starks <straton@lampsacos.demon.co.uk> writes:
> LineList=open(filename,'r').readlines()[1,]


You don't want to do that if the file is very large. Also,
you meant [1:] rather than [1,]

Reply With Quote
  #7  
Old 08-28-2008, 06:01 AM
Chris
Guest
 
Default Re: How to ignore the first line of the text read from a file

On Aug 28, 11:53*am, Ken Starks <stra...@lampsacos.demon.co.uk> wrote:
> youngjin.mich...@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.

>
> LineList=open(filename,'r').readlines()[1,]
> for line in Linelist:
> * * blah blah


That's bad practice as you load the entire file in memory first as
well as it will result in a type error (should be '.readlines()[1:]')
Reply With Quote
  #8  
Old 08-28-2008, 09:56 AM
rurpy@yahoo.com
Guest
 
Default Re: How to ignore the first line of the text read from a file

On Aug 27, 11:12 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net> wrote:
> On Wed, 27 Aug 2008 21:11:26 -0700, youngjin.mich...@gmail.com wrote:
> > 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?

>
> input_file = open(filename)
> lines = iter(input_file)
> lines.next() # Skip line.
> for line in lines:
> print line
> input_file.close()
>
> Ciao,
> Marc 'BlackJack' Rintsch


A file object is its own iterator so you can
do more simply:

input_file = open(filename)
input_file.next() # Skip line.
for line in input_file:
print line,
input_file.close()

Since the line read includes the terminating
EOL character(s), print it with a "print ... ,"
to avoid adding an additional EOL.

If the OP needs line numbers elsewhere in the
code something like the following would work.

infile = open(fileName, 'r')
for lineNumber, line in enumerate (infile):
# enumerate returns numbers starting with 0.
if lineNumber == 0: continue
print line,
Reply With Quote
  #9  
Old 08-28-2008, 10:09 AM
Paul Rubin
Guest
 
Default Re: How to ignore the first line of the text read from a file

rurpy@yahoo.com writes:
> If the OP needs line numbers elsewhere in the
> code something like the following would work.
>
> infile = open(fileName, 'r')
> for lineNumber, line in enumerate (infile):
> # enumerate returns numbers starting with 0.
> if lineNumber == 0: continue
> print line,


This also seems like a good time to mention (untested):

from itertools import islice

for line in islice(infile, 1, None):
print line,
Reply With Quote
  #10  
Old 08-28-2008, 01:16 PM
norseman
Guest
 
Default Re: [Q] How to ignore the first line of the text read from a file

Benjamin Kaplan wrote:
> On Thu, Aug 28, 2008 at 12:11 AM, youngjin.michael@gmail.com <
> youngjin.michael@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.
>>
>> --

>
>
> Files are iterators, and iterators can only go through the object once. Just
> call next() before going in the for loop. Also, don't use "file" as a
> variable name. It covers up the built-in type.
>
> afile = open(file_name, 'r')
> afile.next() #just reads the first line and doesn't do anything with it
> for line in afile :
> print line
>
>
>> http://mail.python.org/mailman/listinfo/python-list
>>

>
>
> ------------------------------------------------------------------------
>
> --
> http://mail.python.org/mailman/listinfo/python-list


==================
actually:
import os

file = open(filename, 'r')
for line in file:
dummy=line
for line in file:
print line


is cleaner and faster.
If you need line numbers, pre-parse things, whatever, add where needed.

Steve
norseman@hughes.net
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 04:01 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.