如何在字典中输出输出?

时间:2014-07-02 05:12:03

标签: python regex string python-2.7 dictionary

我正在linux机器上执行命令,我在变量中获取该命令输出。现在我将如何将其放入字典并使用键访问值?

这是命令输出:

101 Ok, with warnings - Statistics at Mon Jun 30 18:20:43 2014
DHCP Stats: Total counters since: Mon Jun 30 17:34:17 2014
{Category: server}: ack-latency-counts={{0 86157} {1 87} {2 241} {3 0} {4 0} {5 0} {6 0} {7 2}}; acks=86554; acks-per-second=31; active-leases=86553; bootp-received=0; bootp-sent=0; client-class-fails=0; configured-leases=950466; declines=0; discards=0; discovers=105314; dropped-total=7140; duplicates=0; extension-drops=0; extension-errors=0; grace-expirations=0; informs=0; invalid-clients=7140; invalid-packets=0; lease-queries=0; lease-queries-active=0; lease-queries-unassigned=0; lease-queries-unknown=0; naks=0; offer-timeouts=27665; offers=98174; over-max-waiting=0; packets-dropped=0; packets-received=191868; packets-sent=184728; queue-limited-discovers-dropped=0; releases=0; request-buffers-allocated=500; request-buffers-in-use=1; request-dropped-old=0; request-dropped-others=0; requests=86554; reserved-active-leases=0; reserved-leases=0; response-buffers-allocated=1000; response-buffers-in-use=1; response-dropped-old=0; response-dropped-others=0; responses-dropped=0; tcp-active-lease-queries=0; tcp-bulk-lease-queries=0; tcp-connections-dropped=0; tcp-current-connections=0; tcp-lq-active=0; tcp-lq-done=0; tcp-lq-status=0; tcp-lq-status-catchup-complete=0; tcp-lq-status-connection-active=0; tcp-lq-status-data-missing=0; tcp-lq-status-malformed-query=0; tcp-lq-status-not-allowed=0; tcp-lq-status-query-terminated=0; tcp-lq-status-unspec-fail=0; tcp-lq-unassigned=0; tcp-total-connections=0; timeouts=27665; total-scopes=32; unknown-scopes=0;
{Category: failover}: binding-acks-received=0; binding-acks-sent=0; binding-naks-received=0; binding-naks-sent=0; binding-updates-received=0; binding-updates-sent=0; packets-dropped=0; packets-received=0; packets-sent=0; polls-received=0; polls-sent=0; pool-requests-received=0; pool-responses-sent=0; update-done-received=0; update-done-sent=0; update-requests-received=0; update-requests-sent=0;
{Category: dhcpv6}: active-leases=0; advertises=0; allocated-leases=0; auth-fails=0; bulk-leasequeries=0; bulk-leasequery-data=0; bulk-leasequery-done=0; bulk-leasequery-replies=0; client-class-fails=0; confirms=0; declines=0; discards=0; dropped-total=0; duplicates=0; grace-expirations=0; info-requests=0; invalid-clients=0; invalid-packets=0; leasequeries=0; leasequery-replies=0; offer-timeouts=0; over-max-waiting=0; packets-received=0; packets-received-relay=0; packets-sent=0; packets-sent-relay=0; queue-limited-solicits-dropped=0; rebinds=0; reconfigures=0; releases=0; renews=0; replies=0; reply-latency-counts={{0 0} {1 0} {2 0} {3 0} {4 0} {5 0} {6 0} {7 0}}; request-dropped-old=0; request-dropped-others=0; requests=0; reserved-active-leases=0; reserved-leases=0; response-dropped-old=0; response-dropped-others=0; solicits=0; tcp-connections-dropped=0; tcp-current-connections=0; tcp-lq-status-malformed-query=0; tcp-lq-status-not-allowed=0; tcp-lq-status-not-configured=0; tcp-lq-status-query-terminated=0; tcp-lq-status-unknown-query=0; tcp-lq-status-unspec-fail=0; tcp-total-connections=0; total-prefixes=0; unknown-links=0;

1 个答案:

答案 0 :(得分:1)

您有重叠键,因此尝试将所有数据放入单个字典中并不是一个好主意,至少在您没有进一步说明的情况下。 我的想法是将re.sub您的数据转换为有效的JSON,然后使用json模块从中制作三个词典,因为您有三个不同的类别。

import re, json

for i in range(2): # skip first two lines
    raw_input()
data = [raw_input() for i in range(3)]
for i,d in enumerate(data):
    d = re.sub(r'^{Category: \w+}:', '', d)
    d = d.replace(';', ',').replace('=', ':')[:-1]
    d = '{' + re.sub(r'\ ([^:]+):([^,]+)',r'"\1":"\2"', d) + '}'
    data[i] = json.loads(d)

server = data[0]
failover = data[1]
dhcpv6 = data[2]

# Demo
print(dhcpv6['reply-latency-counts'])

用法:

yourcommand | python2.7 program.py

演示(file包含您的数据):

>> cat file | python2.7 program.py 
{{0 0} {1 0} {2 0} {3 0} {4 0} {5 0} {6 0} {7 0}}