<p>你要解决两个问题:</p>
<ul>
<li>把它分成相应的部分</li>
<li>删除重复项</li>
</ul>
<hr/>
<p><strong>拆分:</strong></p>
<p><em>第一个选项</em></p>
<p>逐行分析文件:</p>
<pre><code>parts = [] # all lines between 2 @TestRun's
chunks = [] # all chunks of lines between 2 @TestRun's
startNow = False # wait till first @TestRun before keeping anything
for line in Text(): # see definition for Text() below - it mimics your open('...')
if line.strip() == '@TestRun':
startNow = True
if len(parts) > 0: # found a Testrun, if parts contains lines append to chunks
chunks.append(parts)
parts = []
elif startNow == True: # check if first TestRun hit, if so append line to parts
parts.append(line)
print(chunks) # done -> list of list of lines between chunks.
</code></pre>
<p><em>第二选项</em></p>
<p>不要将文本逐行拆分,将其作为完整文本阅读,并使用列表理解来拆分:</p>
<pre><code>biggerChunks = [x.strip() for x in TextTT().split("@TestRun") ]
chunkified = [x.splitlines() for x in biggerChunks if len(x.strip()) > 0 ]
</code></pre>
<p>首先在<code>@TestRun</code>上拆分,得到一个大文本块列表,然后按行拆分每个文本块。结果大致相同:[[2@TestRun之间的所有行]]</p>
<hr/>
<p><strong>删除重复项</strong>(保持顺序)</p>
<p>这里的答案是:<a href="https://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-in-whilst-preserving-order">how-do-you-remove-duplicates-from-a-list-in-whilst-preserving-order</a>-这是一个SO链接,所以不会在这里重复:)</p>
<hr/>
<p><strong>助手</strong>
Text()用于替换打开的文件,TestTT()是整个文本块:</p>
<pre><code>def Text(): # instead of file open, returns list of lines
return TextTT().splitlines()
def TextTT(): # unsplit text
return '''
@TestRun
And user validate message on screen "Switch to paperless"
And user click on "Manage accounts" label
And user click link with label "View all online services"
And user waits for 10 seconds
Then page is successfully launched
And user click link with label "Go paperless for complete convenience"
Then page is successfully launched
And user validate message on screen "#EmailAddress"
And user clicks on the button "Confirm"
Then page is successfully launched
And user validate message on screen "#MessageValidate"
Then page is successfully launched
And user click on "menu open user preferences" label
And user clicks on the link "Statement and letter preferences"
Then page is successfully launched
And user validate "Switch to paperless" button is disabled
And user validate message on screen "Online only"
When user click on "Log out" label
Then page is successfully launched
@TestRun
And user click on link "Mobile site"
And user set text "#Surname" on textbox name "surname"
Then page is successfully launched
And user click on link "#Account"
Then page is successfully launched
And user verify message on screen "#Account"
And user verify message on screen "Manage statements"
And user verify message on screen "Step 1 of 3"
Then page is successfully launched
And user verify message on screen "Current format type"
And user verify message on screen "Online"
When user selects the radio button "Paper"
@TestRun
Then user wait for page load
And user click on button "Continue to Online Banking"
Then user wait for page load
And user click on "menu open user preferences" label
And user clicks on the link "Statement and letter preferences"
Then page is successfully launched
And page is successfully launched
And user waits for 10 seconds
@TestRun
Then page is successfully launched
And user waits for 10 seconds
And user click checkbox "Telephone"
And user click checkbox "Post"
And user clicks on the button "Save"
Then page is successfully launched
'''
</code></pre>
<p>请参阅注释以获取解释-您可以使用f.e。itertools.chain公司如果需要的话重新组合内线</p>