跳过循环中的最后一行 [英] skip last line in loops

查看:68
本文介绍了跳过循环中的最后一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我怎么能跳过使用循环打印最后一行(for / while)


例如


for open(文件):

打印行。


我想跳过打印文件的最后一行。谢谢


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

推荐答案

ei * **********@yahoo.com 写道:

hi,

我怎么能跳过使用循环打印最后一行(for / while)


例如

打开行中的
(文件):

打印行。


我想跳过打印文件的最后一行。谢谢

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 (文件名)


xlines = afile.xreadlines()

aline = xlines.next

for nextline in xlines:

打印aline

aline = nextline


James

afile = open(filename)

xlines = afile.xreadlines()

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

James


James Stroud写道:
James Stroud wrote:
ei *********** @ yahoo.com 写道:

> hi,
如何跳过使用循环打印最后一行(for / while)

例如

对于打开的行(文件):
打印行。

我想跳过打印文件的最后一行。谢谢
>
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(文件名)


xlines = afile.xreadlines()


aline = xlines.next
$ x $ b for xline的nextline:

print aline

aline = nextline


James


afile = open(filename)

xlines = afile.xreadlines()

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

James



Shoule be


aline = xlines.next()

Shoule be

aline = xlines.next()


尝试:

afile = open(文件名)

lines = afile.readlines()[: - 1]#将除最后一个元素以外的所有元素分配给

a list" lines"

for line in lines:

print line

Try:
afile = open(filename)
lines = afile.readlines()[:-1] # assigns all except the last element to
a list "lines"
for line in lines:
print line


这篇关于跳过循环中的最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆