Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

Trying to parse values out of a string using ttp in Python

I am trying to parse out some values from the results of an easysnmp SNMP query but it is not working as expected.

The following results in an error:

from ttp import ttp
a = "<SNMPVariable value='1' (oid='enterprises.4998.1.1.20.2.33.1.5.16.86.17.126.66.140.34214089.5', oid_index='', snmp_type='INTEGER')>"
t = "<SNMPVariable value='{{value}}' (oid='{{oid}}', oid_index='{{oid_index}}', snmp_type='{{snmp_type}}')>"
p = ttp(a,t)
Traceback (most recent call last):
  File "C:UsersmeAppDataLocalProgramsPythonPython37libsite-packagestptp.py", line 1168, in parse_template_XML
    template_ET = ET.XML(template_text)
  File "C:UsersmeAppDataLocalProgramsPythonPython37libxmletreeElementTree.py", line 1315, in XML
    parser.feed(text)
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 32

If I remove the wedges from the string I do not get an error, but I don't get the values I'm looking for:

from ttp import ttp
a = "SNMPVariable value='1' (oid='enterprises.4998.1.1.20.2.33.1.5.16.86.17.126.66.140.34214089.5', oid_index='', snmp_type='INTEGER')"
t = "SNMPVariable value='{{value}}' (oid='{{oid}}', oid_index='{{oid_index}}', snmp_type='{{snmp_type}}')"
p = ttp(a,t)
p.parse()
p.result()
[[{}]]

What I expect to get for output is:

[[{'value':'1', 'oid':'enterprises.4998.1.1.20.2.33.1.5.16.86.17.126.66.140.34214089.5', 'oid_index'='', 'snmp_type': 'INTEGER'}]]

I know I'm overlooking something obvious, but I cannot figure it out. Any suggestions?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This would parse it properly:

from ttp import ttp
a = "SNMPVariable value='1'(oid='enterprises.4998.1.1.20.2.33.1.5.16.86.17.126.66.140.34214089.5', oid_index='', snmp_type='INTEGER')"
t = "SNMPVariable value='{{value}}' (oid='{{oid}}', oid_index='{{oid_index | re('.*')}}', snmp_type='{{snmp_type}}')"
p = ttp(a,t)
p.parse()
p.result()

Notice in your data oid_index='', TTP by default uses S+ as a regex for variables, but S+ would not match emtpy ''

Alternatively you can use this template:

t = "SNMPVariable value='{{value}}' (oid='{{oid}}', oid_index={{oid_index | strip("'")}}, snmp_type='{{snmp_type}}')"

In that case oid_index={{oid_index | strip("'")}} (no single quotes) will match on '' but strip("'") would remove them.

Both approaches produce same output:

[
    [
        {
            "oid": "enterprises.4998.1.1.20.2.33.1.5.16.86.17.126.66.140.34214089.5",
            "oid_index": "",
            "snmp_type": "INTEGER",
            "value": "1"
        }
    ]
]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...