[英]Python: For loop with files, how to grab the next line within forloop?
I have a file that I want to get each line at a time, but once it gets to a specific line, I need to get the next few lines information. 我有一个文件,我想一次获得每一行,但一旦它到达一个特定的行,我需要获得接下来的几行信息。
Here is a code sample: 这是一个代码示例:
rofile = open('foo.txt', 'r')
for line in rofile:
print line
if(line.strip() == 'foo'):
line = line.next()
print line
line = line.next()
print line
line = line.next()
print line
When I come back around and loop for the second time, that first print statement should print the 5th line in the file. 当我第二次回来并循环时,第一个print语句应该打印文件中的第5行。 Is there any possible way to do this?
有没有办法做到这一点?
EDIT: Sorry for not clarifying the details. 编辑:很抱歉没有澄清细节。
rofile
is a file object that I'm iterating through. rofile
是我正在迭代的文件对象。 Whether next()
is the real method to obtain the next line when using a file, I don't know. next()
是否是使用文件时获取下一行的真正方法,我不知道。 I don't have much experience with file manipulation in python. 我对python中的文件操作没有多少经验。
You can use iter
to convert your object into an iterable which supports next
. 您可以使用
iter
将对象转换为支持next
的iterable。
irofile = iter(rofile)
for line in irofile:
print line
if(line == 'foo'):
line = next(irofile) #BEWARE, This could raise StopIteration!
print line
As pointed out in the comments, if your object is already an iterator, then you don't need to worry about iter
(this is the case with file
objects). 正如在评论中指出,如果你的对象已经是一个迭代器,那么你就不需要担心
iter
(这是与本案file
对象)。 However, I leave it here as it works for the case of any arbitrary iterable (eg list
s). 但是,我把它留在这里,因为它适用于任何可任意迭代的情况(例如
list
s)。
Depending on the type of object that rofile
is, I can think of a couple of ways to do this. 根据
rofile
的对象类型,我可以想到几种方法来做到这一点。
If you can get it to be simply a list of strings that make up the lines of the file: 如果你可以把它简单化为构成文件行的字符串列表:
for index, line in enumerate(rofile):
if line == 'foo':
for a in range(index, index + HOW_MANY_LINES_YOU_WANT):
print rofile[a]
If the file is already an iterable: 如果文件已经是可迭代的:
for line in rofile:
print line
if line == 'foo':
for a in range(3): # Just do it 3 times
print line.next()
# After this happens and the for loop is restarted,
# it will print the line AFTER
You can see in this quickie example I wrote that it'll work this way as an iterable: 您可以在这个快速示例中看到我写道它将以可迭代的方式工作:
>>> k = iter([1,2,3,4])
>>> for a in k:
print 'start loop'
print a
if a == 2:
print 'in if'
print k.next()
print 'end if'
print 'end loop'
start loop
1
end loop
start loop
2
in if
3
end if
end loop
start loop
4
end loop
Don't use a for
loop if you don't actually want to do something for every line. 如果您实际上不想为每一行做某事,请不要使用
for
循环。 One option might be: 一种选择可能是:
try:
while True:
line = file.next()
#do stuff
if line == 'foo':
#do other stuff
except(StopIteration):
#go on with your life
Here's a simple way to do it for a file object: 这是为文件对象执行此操作的简单方法:
with open('foo.txt', 'r') as rofile:
for line in rofile:
print line,
if line.strip() == 'foo':
for _ in xrange(3): # get the next 3 lines
try:
line = rofile.next()
except StopIteration:
break
print line,
The try/except is needed in case there aren't 3 more lines after a 'foo' line is seen. 如果在看到'foo'行之后没有3行,则需要try / except。
我有一个类似的问题,并使用continue
语句代替我的情况
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.