You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python解析路由器MAC地址:处理单/多MAC及无MAC场景

路由器MAC地址解析的多场景处理问题

我正在解析路由器输出数据,需要处理三种场景:单个MAC地址、两个重复MAC地址、无MAC地址。目前的处理代码在应对这些场景时存在问题,以下是相关信息,寻求正确的处理方案:

现有处理代码

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'

路由器输出样例

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

解析后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:
[{}]

需要修正代码,确保在三种场景下都能正确处理:单个MAC时提取该地址,重复MAC时返回唯一值,无MAC时返回'NULL'。

内容的提问来源于stack exchange,提问作者ankou

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 06:39:14