2017-10-18 16 views
0

これを元の投稿と同じように完全に書き直しました。私がしようとしているのは、データを行ごとに解析し、辞書を作成することです。私はこのデータを整理するより良い方法があると思っています。私がこれを考えようとしていた元々の方法は、いくつかのことを説明していなかったので、これを思いついた。私はサービスポリシーの出力を1行ずつループしてInterface、ポリシー名でデータをまとめ、キュー、ドロップ、およびバッファなしのドロップを引き出します。私が抱えている問題は、追加のポリシーを考慮していないため、データの元のパスが上書きされることです。複数のデータソースからのPythonでのネストされた辞書の作成

サービスポリシー出力:

GigabitEthernet11/1 

Service-policy output: Gi11_1 

Counters last updated 7191104 seconds ago 

Class-map: class-default (match-any) 
    0 packets, 0 bytes 
    30 second offered rate 0000 bps, drop rate 0000 bps 
    Match: any 
    Queueing 
    queue limit 33025 packets 
    (queue depth/total drops/no-buffer drops) 0/0/0 
    (pkts output/bytes output) 0/0 
    shape (average) cir 500000000, bc 2000000, be 2000000 
    target shape rate 500000000 

    Service-policy : child 

    Counters last updated 7191104 seconds ago 

    Class-map: class-default (match-any) 
     0 packets, 0 bytes 
     30 second offered rate 0000 bps, drop rate 0000 bps 
     Match: any 
     Queueing 
     queue limit 33025 packets 
     (queue depth/total drops/no-buffer drops) 0/0/0 
     (pkts output/bytes output) 0/0 
     bandwidth remaining ratio 100 

for ints, int_strings in zip(int_names, int_output): 
    counts.setdefault(ints, {}) 

    for line in int_strings.splitlines(): 
     matchpolicy = re.search(r'(Service-policy.*)', line) 
     matchdrops = re.findall(r'total drops.*', line) 
     if matchpolicy: 
      spolicies = matchpolicy.group(0) 
      counts[ints]['Policy'] = spolicies 
     if matchdrops: 
      regx = re.search(r'\s(\d+)\/(\d+)\/(\d+)', line) 
      counts[ints]['queue'] = int(regx.group(1)) 
      counts[ints]['drops'] = int(regx.group(2)) 
      counts[ints]['no-buffer'] = int(regx.group(3)) 

return counts 

私は、追加の深さレベルで辞書を作成しようとしたが、私は[int型] [spolicies]の行数にキーエラーになっています。私が読んだところでは、これはネストされた辞書がどのように働いたかと思ったが、私は誤解していると思っている。

for ints, int_strings in zip(int_names, int_output): 
    counts.setdefault(ints, {}) 

    for line in int_strings.splitlines(): 
     matchpolicy = re.search(r'(Service-policy.*)', line) 
     matchdrops = re.findall(r'total drops.*', line) 
     if matchpolicy: 
      spolicies = matchpolicy.group(0) 
      counts[ints][spolicies] 
     if matchdrops: 
      regx = re.search(r'\s(\d+)\/(\d+)\/(\d+)', line) 
      counts[ints][spolicies]['queue'] = int(regx.group(1)) 
      counts[ints][spolicies]['drops'] = int(regx.group(2)) 
      counts[ints][spolicies]['no-buffer'] = int(regx.group(3)) 

return counts 

どちらの方法でも、このデータを整理するためのより良い方法があると思われますので、後で簡単に処理できます。何か案は?

+1

で投稿してください[「誰かが私を助けることができます?」有効な質問ではありません](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question)。これは通常、あなたが必要とするものは、ローカルの家庭教師と30分、またはスタックオーバーフローではなくチュートリアルを歩くことを示唆しています。 – Prune

答えて

0
labels = ["depth", "drops", "buffer_drops"] 
values = ['0', '14996', '0', '0', '2100', '0'] 
keys=['Gi1','Gi2'] 

values_grouped_by_3 = list(zip(*[iter(values)]*3)) 
data = dict(zip(keys,[dict(zip(labels,vals)) for vals in values_grouped_by_3])) 

チュートリアルと実際の援助の多くを希望の場合、最初の努力をし、あなたの努力と何を期待し、何をお使いの出力が

+0

申し訳ありませんが、私が思いついたすべてのコードは意味をなさないので、私はそれを使って投稿を曇らせたくありませんでした。私はこの時点で私のコードを何度も変更しました。私はどこから始めたのか分かりません。 #for interface_strings、header_strings in header_strings: #interf [header_strings] [interface_strings] .append(outerrs)(私は正しくポストしなかったことを認識しています) – pegruder

関連する問題