I am trying to parse data from an output from a router, however sometimes two duplicate values. Because of this, I need to check for 3 possibilities, one MAC, two MACs and no MAC.
This is what I currently have:
else: #Starts the parse
parser5 = ttp(output5, template_05_ad)
parser5.parse()
#put result in JSON format
results_0 = parser5.result(format='json')[0]
#str to list **convert with json.loads
result_0 = json.loads(results_0)
#Places the results into variables
if 'mac' in result_0[0][0]:
mac_connected = str(result_0[0][0]['mac'])
elif 'mac' in result_0[0]:
mac_connected = str(result_0[0]['mac'])
else:
mac_connected = 'NULL'
With outputs looking like the following:
One MAC:
CE
Vlan Mac Address Type Interface CTag Vlan
---- ----------------- --------- ---------------- ---- ----
4065 E1:B4:19:64:1B:51 Dynamic 10/0/1@1/2/2 na na
Total MAC addresses for this criterion: 1
Two MAC:
CE
Vlan Mac Address Type Interface CTag Vlan
---- ----------------- --------- ---------------- ---- ----
4065 E1:B4:19:64:1B:51 Dynamic 10/0/1@1/2/2 na an
4065 E1:B4:19:64:1B:51 Dynamic 10/0/1@1/2/2 na na
Total MAC addresses for this criterion: 1
No MAC:
No MAC addresses found for this criterion
When it gets parsed it will look like this (printed output from result_0):
One MAC::
[{'mac': 'E1:B4:19:64:1B:51'}]
Two MAC:
[[{'mac': 'E1:B4:19:64:1B:51'}, {'mac': 'E1:B4:19:64:1B:51'}]]
No MAC:
[{}]
Sorry if this has been answered before, any help would be appreciated, thanks.
result_0 = json.loads(results_0)print(result_0)<--