【问题标题】:How can i get the output in a dictionary?我怎样才能得到字典中的输出?
【发布时间】:2014-08-22 18:48:04
【问题描述】:

我正在一台 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;

【问题讨论】:

  • 用两个词:以某种方式删除ack-latency-count 之前的所有内容,而不是由; 拆分,而不是由= 拆分的所有块,左侧部分是键,右侧部分是值。这是给你的问题:你至少尝试过什么吗?
  • “我如何将它放入字典并使用键访问值?” -- 从trying开始:编写一些代码,调试它,再次迭代直到你完成它 - 或者直到你卡住 - 然后然后在这里发布一个更具体的问题.

标签: python regex string python-2.7 dictionary


【解决方案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}}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多