473,378 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

Aug 28 '08 #1
13 25731
On Wed, 27 Aug 2008 21:11:26 -0700, yo**************@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
Aug 28 '08 #2
On Aug 28, 6:11*am, "youngjin.mich...@gmail.com"
<youngjin.mich...@gmail.comwrote:
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
Aug 28 '08 #3
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).
Aug 28 '08 #4
yo**************@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
Aug 28 '08 #5
Ken Starks <st*****@lampsacos.demon.co.ukwrites:
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,]

Aug 28 '08 #6
On Aug 28, 11:53*am, Ken Starks <stra...@lampsacos.demon.co.ukwrote:
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:]')
Aug 28 '08 #7
On Aug 27, 11:12 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
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,
Aug 28 '08 #8
ru***@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,
Aug 28 '08 #9
Benjamin Kaplan wrote:
On Thu, Aug 28, 2008 at 12:11 AM, yo**************@gmail.com <
yo**************@gmail.comwrote:
>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
no******@hughes.net
Aug 28 '08 #10
On Thu, 28 Aug 2008 10:16:45 -0700, norseman wrote:
Benjamin Kaplan wrote:
>On Thu, Aug 28, 2008 at 12:11 AM, yo**************@gmail.com <
yo**************@gmail.comwrote:
>>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.
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.

Ciao,
Marc 'BlackJack' Rintsch
Aug 28 '08 #11


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

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,
I believe that file.readline() will work better than file.next() for
most purposes since the latter will raise StopIteration on an empty file
whereas file.readline() merely returns ''.

Aug 28 '08 #12
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****@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,
and to my *great* surprise the second ugly snippet is consistently a
smidgen faster even with the pointless import and dummy statement left in.

That is so counter-intuitive that I wonder whether I've done something
wrong, or if it's some sort of freakish side-effect of disk caching or
something. But further investigation will have to wait for later.

If anyone wants to run their own timing tests, don't forget to close the
file explicitly, otherwise timeit() will (I think...) simply iterate over
the EOF for all but the first iteration.

--
Steven
Aug 29 '08 #13
On Aug 28, 3:47*am, Santiago Romero <srom...@gmail.comwrote:
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).
# You could also do the following:

from itertools import islice

f = open(filename)

for each_line in islice(f, 1, None):
print line
Aug 30 '08 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Peter Kleiweg | last post by:
I'm still new to Python. All my experience with OO programming is in a distant past with C++. Now I have written my first class in Python. The class behaves exactly as I want, but I would like to...
23
by: FrancisC | last post by:
#include <stdio.h> int file_copy( char *oldname, char *newname ); int main() { char source, destination; printf("\nEnter source file: ");
4
by: Amit Kulkarni | last post by:
Hi, I have small problem. I want to truncate a line in a text file using C file handling functions and write new line in place of it. How do I do it? e.g. "example.txt" Line 1: This is a...
5
by: ma740988 | last post by:
Consider the source: # include <iostream> # include <string> # include <fstream> # include <vector> # include <sstream> using namespace std;
4
by: News | last post by:
Hi Everyone, The attached code creates client connections to websphere queue managers and then processes an inquiry against them. The program functions when it gets options from the command...
4
by: Kim | last post by:
Random image downloader for specified newsgroup. Hi I'm writing a small script that will download random images from a specified newsgroup. I've imported yenc into the script but I can't open the...
0
by: Kim | last post by:
Random image downloader for specified newsgroup. Hi I'm writing a small script that will download random images from a specified newsgroup. I've imported yenc into the script but I can't open the...
0
by: hagar | last post by:
Hi all, I have a problem which I can not understand why this is happening! Debugging this I actually see that it grabs first record then when stepping through code to the line rsImportTo.AddNew...
29
by: gs | last post by:
let say I have to deal with various date format and I am give format string from one of the following dd/mm/yyyy mm/dd/yyyy dd/mmm/yyyy mmm/dd/yyyy dd/mm/yy mm/dd/yy dd/mmm/yy mmm/dd/yy
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.