如何自动创建第二个var?

时间:2017-12-29 13:55:19

标签: python variables

我解析了以下行的文本文件。

stp 11441 0 0 0 0

txt文件中总有2个这样的行出现。我正在寻找行中的第二个值(这里是11441)并将其保存为变量。

我已经弄清楚如何只使用一个变量进行此类操作。 这是我正在使用的代码

import re
with open('cpu.txt', 'r') as file:
    for line in file:
        match = re.search('stp               \d{2,100}', line)
        if match:
            stp_queue1 = match.group().split( )[1]

但是,我无法理解第二次匹配发生时如何指定变量(stp_queue2)。

换句话说: 如果文件包含以下两行:

stp 11441 0 0 0 0
stp 20000 0 0 0 0

然后stp_queue1应为11441,stp_queue2应为20000。

你能帮帮我吗?

5 个答案:

答案 0 :(得分:2)

您可以使用许多模式来解决此问题:

我告诉你三个模式,你可以选择你想要的:

  

第一种模式:

import re

pattern=r'stp\s(\d+)'

output=[]
with open('file.txt','r') as f:
    for line in f:
        match=re.search(pattern,line)
        output.append(match.group(1))

print(output)

输出:

['11441', '20000']
  

模式2:

r'[0-9]{5}'
  

模式3:

Positive Lookbehind (?<=stp\s)

pattern=r'(?<=stp\s)\d+'

答案 1 :(得分:1)

您可以将值添加到字典中,而不是每个都添加到自己的变量中。请参阅下面的代码,将每个匹配项添加到字典中,密钥为stp_queue#,编号从1开始。

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  

然后提取数据dictionary["stp_queue1"]将返回为找到的第一个匹配项存储的值。

更多关于词典的信息:https://docs.python.org/2/tutorial/datastructures.html#dictionaries

答案 2 :(得分:1)

如果您将它们放入列表中,则会保留订单,并且查找就像stp_queue[0]

一样简单
import re
stp_queue = []
with open('cpu.txt', 'r') as file:
    for line in file:
        match = re.search('stp               \d{2,100}', line)
        if match:
            stp_queue.append(match.group().split( )[1])

答案 3 :(得分:0)

如果你只需要一个包含col-sm-4之后的第一个数字的列表,这可能就足够了:

lg

如果您需要检查该行是否以stp开头,只需将该验证添加到理解中:

with open('cpu.txt', 'r') as f:
    stp_queue = [line.split()[1] for line in f]

print(stp_queue)

答案 4 :(得分:-1)

在正则表达式中使用groups。 请阅读python正则表达式文档

相关问题