473,597 Members | 2,459 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[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 25756
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.clos e()

Ciao,
Marc 'BlackJack' Rintsch
Aug 28 '08 #2
On Aug 28, 6:11*am, "youngjin.mich. ..@gmail.com"
<youngjin.mich. ..@gmail.comwro te:
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(fileI nput):
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(f ilename,'r').re adlines()[1,]
for line in Linelist:
blah blah
Aug 28 '08 #5
Ken Starks <st*****@lampsa cos.demon.co.uk writes:
LineList=open(f ilename,'r').re adlines()[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...@lampsa cos.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(f ilename,'r').re adlines()[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.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.clos e()

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.clos e()

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

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

Similar topics

6
2308
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 get comments about coding style. I'm especially unsure about how a class should be documented, what to put in, and where. When to use double quotes, and when single. For instance, the doc string at the top must be in double quotes, or else the...
23
4089
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
2919
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 text file. Line 2: Second line of it.
5
8677
by: ma740988 | last post by:
Consider the source: # include <iostream> # include <string> # include <fstream> # include <vector> # include <sstream> using namespace std;
4
2381
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 line. It also works when pulling the options from a file.
4
3072
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 image or save it. This is my first script so be gentle! Heres the script ####random group downloader#### import nntplib import string, random import mimetools import StringIO
0
1522
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 image or save it. This is my first script so be gentle! Heres the script ####random group downloader#### import nntplib import string, random import mimetools import StringIO
0
4106
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 it drops first record and grabs second record and continues on no problems (but no 1st record in data set) I am reading a text file record 1 is a top of text file. see code below Private Sub CmdFetchNewData_Click() on Error Goto CmdfetchErr Dim...
29
2882
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
8040
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8259
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6698
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3889
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3932
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1495
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1243
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.