擅长:python、mysql、java
<p>使用<a href="https://more-itertools.readthedocs.io/en/latest/api.html#more_itertools.split_before" rel="nofollow noreferrer">^{<cd1>}</a>第三方库,我们可以在所需目标之前分割文本。你知道吗</p>
<p><strong>更新</strong>:我们可以使用<a href="https://docs.python.org/3/library/itertools.html#itertools.dropwhile" rel="nofollow noreferrer">^{<cd2>}</a>在第一个目标之前删除行。你知道吗</p>
<pre><code>import itertools as it
import more_itertools as mit
with open("CustPref.txt", "r") as f:
lines = f.readlines()
pred = lambda x: x.startswith("@TestRun") # trailing-space protection
inv_pred = lambda x: not pred(x)
lines = it.dropwhile(inv_pred, lines) # optional
chunks = list(mit.split_before(lines, pred))
print(chunks)
</code></pre>
<p>输出(缩写)</p>
<pre><code>[['@TestRun\n',
' And user validate message on screen "Switch to paperless" \n',
...],
['@TestRun \n',
' And user click on link "Mobile site" \n',
...],
['@TestRun\n',
'Then user wait for page load\n',
...],
...]
</code></pre>