【发布时间】:2018-04-22 21:23:30
【问题描述】:
我有台词:
for line in f:
if 'ipsum' in next(f): #just want to check
print("Hello")
我想要的是只检查下一行的内容,而不是整体跳转到下一行。我想要的是这样的:
for line in f:
if 'ipsum' in next(f): #just checking
print("Hello")
next(f) #then actually jump to the next line
还有其他方法可以做到这一点吗?
【问题讨论】:
-
''' junk.txt 是:一二三...十''' import sys filehandle = open("junk.txt", "r") lines = filehandle.readlines() currentline = 0 for line in lines: if currentline == len(lines) - 1: continue nextline = currentline + 1 if "three" in lines[nextline]: print (line) currentline += 1 #试着回答这个,写了起来,现在无法提交答案:(这行得通。
标签: python python-3.x