由于原始文章不清楚,所以将其完全改写。我想要做的是逐行解析一些数据并创建一个字典。我想有更好的方法来组织这些数据。我试图去解决这个问题的原始方式并没有解释几件事情,所以我提出了这个问题。我通过逐行循环服务策略输出来将接口,策略名称的数据放在一起,然后拉出队列,丢弃和无缓冲区丢弃。我遇到的问题是它没有考虑额外的策略,因此数据的原始传递被覆盖。从多个数据源在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