擅长:python、mysql、java
<p>假设文件名为<code>file.txt</code>,则可以获取所有行,并使用regex获取所需的所有数据</p>
<pre><code>import re
with open('file.txt') as f:
contents = f.readlines() # get the lines in a list
for x in contents: # iterate through each line
matched = re.search(r'\S+ out$', x) # find results
if matched:
result.append(matched.group().split(" ")[0]) # save results
print(result)
</code></pre>
<p>结果:</p>
<p><code>['blu330_NWK', 'grn200_NWK']</code></p>
<p>这将通过获取文件中的所有行来打印您想要的所有结果,然后在其中循环,找到文本并在其中保存一个名为<code>results</code>的列表。然后您可以使用它来获取变量中的值</p>
<p>我相信这个解决方案更容易理解,因为它只是在各行之间循环并找到结果</p>