简体   繁体   English

用于输出命令处理的Python字典

[英]Python dictionary for output command handling

I get below output to variable "node_info" 我得到下面的输出到变量“ node_info”

Node: node1
Port: a0a-180
Link: up
MTU: 9000
1 entries found

I want check the status of the Port, Link, and MTU 我要检查端口,链接和MTU的状态

I was using below method: 我正在使用以下方法:

def mkdict(din):
    global d
    d = {}
    for line in din.split("\n"):
        if ":" not in line:
                continue
        key, value = line.strip().split(":", 1)
        d[key] = value
mkdict(node_info)
Port = "a0a-180"
Link = "up"
MTU = "9000"

for k, v in d.iteritems():
    if k == "Port":
        v = v.strip()
        if v.lower() == Port.lower():
             print "Port is {}".format(v)
        else:
             print "Failed:Port is {}".format(v)

for k, v in d.iteritems():
    if k == "Link":
        v = v.strip()
        if v.lower() == Link.lower():
             print "Link is {}".format(v)
        else:
             print "Failed:Link is {}".format(v)
for k, v in d.iteritems():
    if k == "MTU":
        v = v.strip()
        if v.lower() == MTU.lower():
             print "MTU is {}".format(v)
        else:
             print "Failed:MTU is {}".format(v)

Now I am getting below output to variable "node_info" 现在,我得到下面的输出到变量“ node_info”

Node: node1
Port: a0a-180
Link: up
MTU: 9000

Node: node2
Port: a0a-180
Link: up
MTU: 9000

Node: node3
Port: a0a-180
Link: up
MTU: 9000

Node: node4
Port: a0a-180
Link: up    
MTU: 9000
4 entries found.

Since keys are repeatable and Node key also repeating. 由于键是可重复的,而节点键也是可重复的。 How to check my output for all 4 nodes (node1 to node4). 如何检查所有4个节点(node1到node4)的输出。 I May get output some times more than 64 nodes. 我可能有时会获得超过64个节点的输出。

Note: actual output of node is like below but i want to check only Port, MTU, Linux hence i provided above info in output . 注意:节点的实际输出如下所示,但我只想检查Port,MTU,Linux,因此我在输出中提供了以上信息。

Node: node1
Port: a0a-180
Link: up
MTU: 9000
status: online
Mac: 00:0a:0c:0a:00:01 
Mode: RW
Type: 10G

Thanks, 谢谢,

Why don't you use a struct like this? 为什么不使用这样的结构?

{key=node_name, value={port:port_val,link:link_val,mtu:mtu_val,status:status_val}}

BTW, what do you mean by this Node key also repeating ? 顺便说一句,这个Node键又重复一次是什么意思? You mean that maybe the node_name(eg: node1) may appears more than once in your input file? 您的意思是也许node_name(例如:node1)可能在您的输入文件中出现多次?

If so, I think it depends on your demands, as I think, for one single node, all the port value, MTU value, etc, should have only one value, so if you get two nodes named node1, what's the accurate value of this node? 如果是这样,我认为这取决于您的需求,就像我认为的那样,对于一个单个节点,所有端口值,MTU值等都应该只有一个值,因此,如果您获得两个名为node1的节点,则准确的值是多少?这个节点?

Code: 码:

data = dict()

with open("source.txt", 'r') as fr:
    while(True):
        line = fr.readline()
        if not line:
            break
        else:
            tmp_array = line.strip().split(":")
            if len(tmp_array) != 2:
                continue
            else:
                key = tmp_array[0]
                value = tmp_array[1]
            if key == "Node":
                node_name = value
                if value not in data:
                    data[value] = dict()
            else:
                if node_name:
                    data[node_name][key] = value

res_num = 0
Port = "a0a-180"
Link = "up"
MTU = "9000"

for key, value in data.iteritems():
    flag = True
    for k, v in value.iteritems():
        if k == "Port":
            if v.strip() != Port:
                print "Failed:{} Port is {}".format(key, v)
                flag = False
        if k == "Link":
            if v.strip() != Link:
                print "Failed:{} Link is {}".format(key, v)
                flag = False
        if k == "MTU":
            if v.strip() != MTU:
                print "Failed:{} MTU is {}".format(key, v)
                flag = False
    if flag:
        res_num += 1

print "{} entries found".format(res_num)

Here is the sample input file: 这是样本输入文件:

Node: node1
Port: a0a-180
Link: up
MTU: 9000

Node: node2
Port: a0a-180
Link: down
MTU: 9000

Node: node3
Port: a0a-180
Link: up
MTU: 9000

Node: node4
Port: a0a-180
Link: up
MTU: 9000

And the output is like: 输出是这样的:

Failed: node2 Link is  down
3 entries found

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM