从命令输出字符串中提取特定元素

时间:2016-03-31 07:29:29

标签: python string python-2.7 dictionary

我有一个命令输出一些不断变化的数据。 在这个数据中,我需要提取一些信息并从中构建一个字典。 我特意寻找那些名称为" quorum"和" quorum-manager"

下面的

是命令的示例输出:

grant all privileges on *.* to user1@'%' identified by 'user1';

我正在寻找类似下面的东西:

GPFS cluster information
========================
  GPFS cluster name:         codev.NSD-1
  GPFS cluster id:           8865240017152489758
  GPFS UID domain:           codev.NSD-1
  Remote shell command:      /usr/bin/ssh
  Remote file copy command:  /usr/bin/scp
  Repository type:           CCR

 Node  Daemon node name  IP address   Admin node name  Designation
-------------------------------------------------------------------
   1   NSD-1             192.168.0.1  NSD-1            quorum
   2   NSD-2             192.168.0.2  NSD-2            quorum-manager
   3   NSD-3             192.168.0.3  NSD-3            quorum-manager
   4   NSD-4             192.168.0.4  NSD-4            manager
   5   client-1          192.168.0.5  client-1

有一种pythonic方式来做到这一点。如果我必须使用可能太麻烦的正则表达式。 任何帮助表示感谢。

2 个答案:

答案 0 :(得分:0)

正则表达式非常适合这项任务:

results = {}
with open('command_output') as fd:
    for line in fd:
        match = re.fullmatch('\s+[0-9]+\s+([^\s]+)\s+[0-9\.]+\s+([^\s]+)\s+([a-z-]+)', line)
        if match:
            daemon, admin, designation = match.groups()
            results[daemon] = designation
print(results)

另一种解决方案是创建解析器,或者只使用split:

results = {}
with open('command_output') as fd:
    for line in fd:
        node, daemon, ip, admin, designation = line.split()
        results[daemon] = designation
print(results)

答案 1 :(得分:0)

你可以使用Python字符串'表行上的split()方法:

nodes = []

for line in table_lines:
    # line = "  1   NSD-1             192.168.0.1  NSD-1            quorum"

    (node, daemon_node, ip, admin_node, designation) = line.split()

    if designation in ('quorum', 'quorum-manager'):
        nodes.append("{}: {}".format(admin_node, designation))

# Print the selected nodes as comma separated list
print ", ".join(nodes)

另一种方法是使用简单的支持类来解析行和列表推导:

class NodeInfo(list):
    '''
    This class provides property access to the table columns
    and string formatting for the output.
    '''

    @property
    def node(self):
        return self[0]

    @property
    def daemon_node(self):
        return self[1]

    @property
    def ip(self):
        return self[2]

    @property
    def admin_node(self):
        return self[3]

    @property
    def designation(self):
        if len(self) > 4:
            return self[4]
        return ""

    def __str__(self):
        return "{}: {}".format(self.admin_node, self.designation)

designations = ('quorum', 'quorum-manager')

nodes = [NodeInfo(line.split()) for line in table_lines]
selected_nodes = filter(lambda n: n.designation in designations, nodes)

print ", ".join([str(node) for node in selected_nodes])