これを元の投稿と同じように完全に書き直しました。私がしようとしているのは、データを行ごとに解析し、辞書を作成することです。私はこのデータを整理するより良い方法があると思っています。私がこれを考えようとしていた元々の方法は、いくつかのことを説明していなかったので、これを思いついた。私はサービスポリシーの出力を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
どちらの方法でも、このデータを整理するためのより良い方法があると思われますので、後で簡単に処理できます。何か案は?
で投稿してください[「誰かが私を助けることができます?」有効な質問ではありません](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question)。これは通常、あなたが必要とするものは、ローカルの家庭教師と30分、またはスタックオーバーフローではなくチュートリアルを歩くことを示唆しています。 – Prune