擅长:python、mysql、java
<p>以下代码将打印出所需内容:</p>
<pre><code>fname = "file.txt"
with open(fname) as f:
headings = []
for line in f:
if not line.startswith(' '):
current_heading = line.strip()
else:
if 'value yes never' in line:
headings.append(current_heading)
print(headings)
</code></pre>
<p>如果希望文本“value yes never”出现在每个标题之间的多行上,则可以在结尾处执行重复数据消除:</p>
<pre><code>print(set(headings))
</code></pre>
<p>或在追加之前添加检查:</p>
<pre><code>if current_heading not in headings:
headings.append(current_heading)
</code></pre>