Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ttp template
#1
I am using ttp template to parse a data from command line

This is my code
import json
from ttp import ttp
temp="""

System Info{{_start_}}
IPv4 configuration : {{ipv4_configuration}}
IPv6 configuration : {{ipv6_configuration}}

"""
data_to_parse="""

System Info
IPv4 configuration : Disabled
IPv6 configuration : Disabled


"""
parser = ttp(data=data_to_parse, template=temp)
parser.parse()

result = json.loads(parser.result(format="json")[0])[0]

print(result)
My output is this

Output:
{ipv4_configuration:'Disabled'}
why is ipv6 not appearing in output?
Reply
#2
I figured out what it was doing right away, but it took a while to find in the documentation. Unless told otherwise, ttd replace digits with \d, so 'IPV4 configuration' and 'IPv6 configuration' make identical patterns. I finally found it under Indicators.

https://ttp.readthedocs.io/en/latest/Mat...ght=digits

_exact_
{{ name | _exact_ }}

By default, the parser will replace all digits in a template with the ‘d+’ pattern. However, if _exact_ is present in any match variable within a line, digits will remain unchanged and parsed as is.

This is an important consideration for when a pattern contains numbers.

Your pattern should be:
import json
from ttp import ttp

temp = """
System Info
IPv4 configuration : {{ ipv4_configuration |  _exact_ }}
IPv6 configuration : {{ ipv6_configuration |  _exact_ }}
"""

data_to_parse = """
System Info
IPv4 configuration : Disabled
IPv6 configuration : Enabled
"""
parser = ttp(data=data_to_parse, template=temp)
parser.parse()

result = json.loads(parser.result(format="json")[0])[0]
print(result)
jss likes this post
Reply
#3
(Aug-22-2023, 11:50 PM)deanhystad Wrote: I figured out what it was doing right away, but it took a while to find in the documentation. Unless told otherwise, ttd replace digits with \d, so 'IPV4 configuration' and 'IPv6 configuration' make identical patterns. I finally found it under Indicators.

https://ttp.readthedocs.io/en/latest/Mat...ght=digits

_exact_
{{ name | _exact_ }}

By default, the parser will replace all digits in a template with the ‘d+’ pattern. However, if _exact_ is present in any match variable within a line, digits will remain unchanged and parsed as is.

This is an important consideration for when a pattern contains numbers.

Your pattern should be:
import json
from ttp import ttp

temp = """
System Info
IPv4 configuration : {{ ipv4_configuration |  _exact_ }}
IPv6 configuration : {{ ipv6_configuration |  _exact_ }}
"""

data_to_parse = """
System Info
IPv4 configuration : Disabled
IPv6 configuration : Enabled
"""
parser = ttp(data=data_to_parse, template=temp)
parser.parse()

result = json.loads(parser.result(format="json")[0])[0]
print(result)

Thanks deanhystad.The solution provided worked
Reply
#4
Hi @deanhystad

For the same scenario as below,where we have a description in number of lines,How do we retrieve all the lines.I used _line_ but it gave the information of only the first line

import json
from ttp import ttp
 
temp = """
System Info
IPv4 configuration : {{ ipv4_configuration |  _exact_ }}
IPv6 configuration : {{ ipv6_configuration |  _exact_ }}
Description           : {{description | _line_}}
"""
 
data_to_parse = """
System Info
IPv4 configuration : Disabled
IPv6 configuration : Enabled
Description           : It is used to connect to the network 
                             using the IPV6 and IPV4 which is 
                             secure
"""
parser = ttp(data=data_to_parse, template=temp)
parser.parse()
 
result = json.loads(parser.result(format="json")[0])[0]
print(result)
My output is
Output:
{ipv4_configuration:'Disabled',ipv6_configuration:'Disabled',description': ' It is used to connect to the network '}
Reply
#5
import json
from ttp import ttp

temp = """
System Info
IPv4 configuration : {{ ipv4_configuration |  _exact_ }}
IPv6 configuration : {{ ipv6_configuration |  _exact_ }}
Description        : {{ _start_}}
{{ description | _line_ }}
"""

data_to_parse = """
System Info
IPv4 configuration : Disabled
IPv6 configuration : Enabled
Description        : It is used to connect to the network
                             using the IPV6 and IPV4 which is
                             secure
"""
parser = ttp(data=data_to_parse, template=temp)
parser.parse()

result = json.loads(parser.result(format="json")[0])[0]
print(result)
jss likes this post
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020