由於原始文章不清楚,所以將其完全改寫。我想要做的是逐行解析一些數據並創建一個字典。我想有更好的方法來組織這些數據。我試圖去解決這個問題的原始方式並沒有解釋幾件事情,所以我提出了這個問題。我通過逐行循環服務策略輸出來將接口,策略名稱的數據放在一起,然後拉出隊列,丟棄和無緩衝區丟棄。我遇到的問題是它沒有考慮額外的策略,因此數據的原始傳遞被覆蓋。從多個數據源在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
我試圖創建一個額外的深度級別的字典,但我對數得到一個關鍵的錯誤[整數] [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
無論哪種方式,我猜想有可能是一個更好的方式來組織這個數據,所以我可以以後通過它更容易。有任何想法嗎?
[「有人可以幫助我?」是不是一個有效的SO問題](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question)。這通常表明,你需要的是半個小時的時間與當地的導師,或通過一個教程,而不是堆棧溢出。 – Prune