473,695 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9238
ei***********@y ahoo.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.xreadline s()

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

James
Dec 15 '06 #2
James Stroud wrote:
ei***********@y ahoo.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.xreadline s()

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***********@y ahoo.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***********@y ahoo.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***********@y ahoo.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***********@y ahoo.com wrote:
Fredrik Lundh wrote:
>ei***********@y ahoo.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***********@y ahoo.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(op en("file")):
print line
Dec 15 '06 #10

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

Similar topics

2
585
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 = csv.reader(file(filename)) for row in reader: if (ignoreFirstLine & line == 0):
0
1523
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 using this command-line option? The manual also states the following: "concurrent_inserts If ON (the default), MySQL will allow you to use INSERT on MyISAM tables at the same time as you run SELECT queries on them. You can turn this option
3
26353
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
5764
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 macros */ #ifndef PUBLIC #define PUBLIC
4
4721
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 "smells bad" and just doesn't look right. I was reading about generators. If I was using one, I could do a .next() after the match and get rid of the flags. Any suggestions how I can improve this? inPmos = False
2
15117
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 on a row by row basis. But is there a way to skip the loop and do it as a bulk insert? It's easy to do in Access, but I'm curious to know if SQL Server proper can handle like this. I am guessing that a looping operation would be slower to...
18
6266
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 again skip these 9 lines. how to solve this. and also i wana distinguish these float data and unsigned data, as you can see in data file. after 9 lines, 6 lines are the float data, after this 6 lines are unsigned int. actually this file has a...
10
1664
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 skip the element with id=”Div1” and next reader.Read() reads “ includeme” but i expected it to read element <a id ="Div2" style="visibility:hidden "> and skip it instead its writing “ includeme” to the console ...
9
8307
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
8635
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8574
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9116
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8990
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8850
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
8829
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
5839
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();...
1
3007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1977
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.