擅长:python、mysql、java
<ul>
<li>您需要重新初始化<code>data</code></li>
</ul>
<pre><code>check = False
data = ''
start = 'ear'
end = 'rea'
with open('temp.txt', 'r') as rf:
lines = rf.readlines()
for i,x in enumerate(lines):
if start in x:
check = True
if check:
data += str(x)
if end in x:
check = False
print(data)
print(i)
data = ''
</code></pre>
<p>输出:</p>
<pre><code>ear
hello
hello
hello
rea
5
ear
world
world
rea
9
</code></pre>
<ul>
<li><strong>注意</strong>:这不会打印4。从输出来看,似乎您要打印数据的长度。在这方面你的逻辑是错误的</李>
</ul>