擅长:python、mysql、java
<p>您可以将值添加到字典中,而不是每个值都添加到自己的变量中。请参阅下面的代码,将每个匹配项添加到一个字典中,其中键为stp#u queue,数字从1开始。你知道吗</p>
<pre><code>import re
dictionary={}
with open('cpu.txt', 'r') as file:
counter=1
for line in file:
match = re.search('stp \d{2,100}', line)
if match:
dictionary["stp_queue"+str(counter)] = match.group().split( )[1]
counter++
print dictionary
</code></pre>
<p>然后要提取数据<code>dictionary["stp_queue1"]</code>将返回为找到的第一个匹配项存储的值。你知道吗</p>
<p>更多关于字典的信息:<a href="https://docs.python.org/2/tutorial/datastructures.html#dictionaries" rel="nofollow noreferrer">https://docs.python.org/2/tutorial/datastructures.html#dictionaries</a></p>