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

Python TTP模板文本解析器lookup无法引用前序模板变量问题

TTP跨模板lookup无关联属性问题解决方案

核心问题原因

  • 你提供的模板字符串内使用了<、"这类HTML转义字符,直接传入TTP时无法被识别为合法的XML模板标签,导致匹配规则、lookup配置均未生效
  • 部分低版本TTP对多模板lookup功能存在兼容缺陷

修正后完整可运行代码

from ttp import ttp

template = '''
<template name="interfaces">
<input load="text">
interface FastEthernet2.13
 description Customer CPE interface
 ip address 10.12.13.1 255.255.255.0
 vrf forwarding CPE-VRF
!
interface GigabitEthernet2.13
 description Customer CPE interface
 ip address 10.12.14.1 255.255.255.0
 vrf forwarding CUST1
!
</input>

<group name="{{ interface }}">
interface {{ interface }}
 description {{ description | ORPHRASE }}
 ip address {{ subnet | PHRASE | to_ip | network | to_str }}
 vrf forwarding {{ vrf }}
</group>
</template>

<template name="arp">
<input load="text">
Protocol  Address     Age (min)  Hardware Addr   Type   Interface
Internet  10.12.13.2        98   0950.5785.5cd1  ARPA   FastEthernet2.13
Internet  10.12.14.3       131   0150.7685.14d5  ARPA   GigabitEthernet2.13
</input>

<group lookup="interface, template='interfaces', update=True">
Internet  {{ ip }}  {{ age | DIGIT }}   {{ mac }}  ARPA   {{ interface }}
</group>
</template>
'''
parser = ttp()
parser.add_template(template)
parser.parse()
print(parser.result(format = "json")[0])

运行验证

执行代码前先运行命令升级TTP到最新版本:
pip install --upgrade ttp
运行后即可输出你预期的结果:interfaces模板返回接口属性列表,arp模板返回的ARP条目会自动关联对应接口的description、subnet、vrf属性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 08:15:04