473,396 Members | 1,914 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,396 software developers and data experts.

skip last line in loops

hi,
how can i skip printing the last line using loops (for /while)

eg

for line in open("file):
print line.

I want to skip printing last line of the file.thanks

Dec 15 '06 #1
15 9152
ei***********@yahoo.com wrote:
hi,
how can i skip printing the last line using loops (for /while)

eg

for line in open("file):
print line.

I want to skip printing last line of the file.thanks
afile = open(filename)

xlines = afile.xreadlines()

aline = xlines.next
for nextline in xlines:
print aline
aline = nextline

James
Dec 15 '06 #2
James Stroud wrote:
ei***********@yahoo.com wrote:
>hi,
how can i skip printing the last line using loops (for /while)

eg

for line in open("file):
print line.

I want to skip printing last line of the file.thanks

afile = open(filename)

xlines = afile.xreadlines()

aline = xlines.next
for nextline in xlines:
print aline
aline = nextline

James
Shoule be

aline = xlines.next()
Dec 15 '06 #3
Try:
afile = open(filename)
lines = afile.readlines()[:-1] # assigns all except the last element to
a list "lines"
for line in lines:
print line

Dec 15 '06 #4
ei***********@yahoo.com wrote:
how can i skip printing the last line using loops (for /while)

eg

for line in open("file):
print line.

I want to skip printing last line of the file.
do it lazily:

last_line = None
for line in open("file):
if last_line:
print last_line
last_line = line

or just gobble up the entire file, and slice off the last item:

for line in list(open("file"))[:-1]:
print line

</F>

Dec 15 '06 #5

Fredrik Lundh wrote:
ei***********@yahoo.com wrote:
how can i skip printing the last line using loops (for /while)

eg

for line in open("file):
print line.

I want to skip printing last line of the file.

do it lazily:

last_line = None
for line in open("file):
if last_line:
print last_line
last_line = line

or just gobble up the entire file, and slice off the last item:

for line in list(open("file"))[:-1]:
print line

</F>
hi
would it be a problem with these methods if the file is like 20Gb in
size...?

Dec 15 '06 #6
ei***********@yahoo.com wrote:
>do it lazily:

last_line = None
for line in open("file):
if last_line:
print last_line
last_line = line

or just gobble up the entire file, and slice off the last item:

for line in list(open("file"))[:-1]:
print line

</F>

hi
would it be a problem with these methods if the file is like 20Gb in
size...?
The second one would be a problem, since it creates a list containing all
the lines of the file. Use the lazy approach.

--
Roberto Bonvallet
Dec 15 '06 #7
ei***********@yahoo.com wrote:
Fredrik Lundh wrote:
>ei***********@yahoo.com wrote:
>>how can i skip printing the last line using loops (for /while)

eg

for line in open("file):
print line.

I want to skip printing last line of the file.
do it lazily:

last_line = None
for line in open("file):
if last_line:
print last_line
last_line = line

or just gobble up the entire file, and slice off the last item:

for line in list(open("file"))[:-1]:
print line

</F>

hi
would it be a problem with these methods if the file is like 20Gb in
size...?
See the documentation for xreadlines.

James
Dec 15 '06 #8
James Stroud wrote:
See the documentation for xreadlines.
why?

</F>

Dec 15 '06 #9
ei***********@yahoo.com writes:
for line in open("file):
print line.

I want to skip printing last line of the file.thanks
def all_but_last(it): # yield all but last item of an iterator
a = it.next()
for b in it:
yield a
a = b

for line in all_but_last(open("file")):
print line
Dec 15 '06 #10
On 14 Dec 2006 22:47:23 -0800, ei***********@yahoo.com wrote:
>hi,
how can i skip printing the last line using loops (for /while)

eg

for line in open("file):
print line.

I want to skip printing last line of the file.thanks
while True:
line1 = myfile.readline()
if not line1: break
line2 = myfile.readline()
if line2:
print line1
else:
break
Dan
Dec 15 '06 #11
ei***********@yahoo.com wrote:
>do it lazily:

last_line = None
for line in open("file):
if last_line:
print last_line
last_line = line

or just gobble up the entire file, and slice off the last item:

for line in list(open("file"))[:-1]:
print line

</F>

would it be a problem with these methods if the file is like 20Gb in
size...?
not with the lazy version, of course. the "gobble up" version will
load the entire file into memory.

but cutting off a single line from a 20 gigabyte file by looping over
it sounds like a bit contrived, really. if you're really doing this
(why?), maybe you should just truncate the file in place instead.

</F>

Dec 15 '06 #12
Fredrik Lundh wrote:
James Stroud wrote:
>See the documentation for xreadlines.

why?

</F>
5.16 xreadlines -- Efficient iteration over a file
Dec 15 '06 #13
James Stroud wrote:
Fredrik Lundh wrote:
>James Stroud wrote:
>>See the documentation for xreadlines.

why?

</F>
5.16 xreadlines -- Efficient iteration over a file
http://www.python.org/dev/peps/pep-0004/

The cheat sheet for the effbot quiz :-)

Peter
Dec 15 '06 #14
lines = open('blah').readlines()
for i in range(0, len(lines)-1) :
print lines[i]

ei***********@yahoo.com wrote:
hi,
how can i skip printing the last line using loops (for /while)

eg

for line in open("file):
print line.

I want to skip printing last line of the file.thanks
Dec 17 '06 #15
See the documentation for xreadlines.
>
James
Hmm...

This method returns the same thing as iter(f). New in version 2.1.
Deprecated since release 2.3. Use "for line in file" instead.

--
Posted via a free Usenet account from http://www.teranews.com

Dec 18 '06 #16

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

Similar topics

2
by: Ramon Felciano | last post by:
Hi -- I'm using the csv module to parse a tab-delimited file and wondered whether there was a more elegant way to skip an possible header line. I'm doing line = 0 reader =...
0
by: TK | last post by:
Hello, skip-new is explained as follows in the manual: "Don't use new, possibly wrong routines." (4.1.1 mysqld Command-line Options). Is there a list of the routines that will be disabled when...
3
by: puzzlecracker | last post by:
I want to read lines and skip blank lines: would this work considering the lines can contain tabs, spaces, etc.? file.in: ------ line1 line2
24
by: Robin Cole | last post by:
I'd like a code review if anyone has the time. The code implements a basic skip list library for generic use. I use the following header for debug macros: /* public.h - Public declarations and...
4
by: Jacob Rael | last post by:
I am new to python and I love it. I am hacking a file. I want to not print a line if it contains the word 'pmos4_highv'. I also don't want to print the next line. The following code works but it...
2
by: nano | last post by:
Does sql server have a way to handle errors in a sproc which would allow one to insert rows, ignoring rows which would create a duplicate key violation? I know if one loops one can handle the error...
18
by: luckyyyyyy | last post by:
I appriate your work but... guys i have another problm.....i wana skip first 9 lines from my data file and after skipping i wana read x,y,z at the end of file. i did but after while loop is end it...
10
by: jambalapamba | last post by:
I am trying to skip element in xml document where style attribute is VISIBILITY: hidden or DISPLAY: none. In the following example i want to skip the last two elements but when i...
9
by: stomba | last post by:
Hi, I have a file in this format : 0 3 - - 10 10 1 0 0 0 11 11 I want to read this file and store in a matrix those values:
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...

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.