我似乎无法跳过空行 -
import re
with open('list.txt', 'r+') as f:
line = f.readline()
while(line):
if line != ['']:
if " win" in line:
print(f.readline(),end="")
# 2nd line
print(f.readline(),end="")
# 3rd line
print(f.readline(),end="")
line = f.readline()
LIST.TXT
You tell yourself...
That should have been my big win.
It's a kick in the gut.
Knowing in your heart the reason.
While you're stuck on the outside.
Grinning.
它打印如下,而不是 - 线条在空行后显示。
It's a kick in the gut.
Knowing in your heart the reason.
答案 0 :(得分:0)
您需要告诉它在打印时跳过空行(注释是内联的):
if " win" in line:
for _ in range(3): # do three times
line = f.readline() # get next line
# skip blank lines
while not line.strip():
line = f.readline()
# Print the non-blank line
print(line, end="")
line = f.readline()