我在python中遇到了这个循环的问题:
i = 1
while True:
with open('/tmp/file.txt', 'r+') as f:
for line in f:
work = 'word1' + line + 'word2' + line + 'counter=' + str(i) + 'test'
result = Function()
if "statement" in result:
out = open('/tmp/result.txt', 'a+')
out.write(result)
out.close()
i = i + 10
else:
return i
我想先逐行读取file.txt,然后逐行读取每个行数i
,直到语句存在于结果中,但这个循环是不定式的......所以我删除了break
并使用了{{1相反。但没有结果
当读取file.txt中的所有行并且每个行计数器完成时,如何判断True循环何时停止?
我想要处理的内容:
return i
由于
答案 0 :(得分:0)
我认为你需要切换你的循环:
i = 1
with open('/tmp/file.txt', 'r+') as f:
for line in f:
result = ...
while "statement" in result:
with open('/tmp/result.txt', 'a+') as out:
out.write(result)
i += 10
result = ...
虽然目前还不清楚这个算法应该做什么
答案 1 :(得分:0)
我找到了答案:
out = open('/tmp/result.txt', 'a+')
with open('/tmp/file.txt', 'r+') as f:
for line in f:
i = 1
line=line.strip('\r\n ')
while True:
work = 'word1' + line + 'word2' + line + 'counter=' + str(i) + 'test'
print work
result = Function()
if "statement" in result:
out.write(result)
i += 10
else:
break
out.close()