Python Forum
Using TTP to extract an unknow number of words from a line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using TTP to extract an unknow number of words from a line
#5
If ttp uses re, why not just use re and have more control?

I assume the key words are fixed: keys = ['link-aggregate', 'AdminState', 'OperState', 'Member Ports', 'Description'] If not, find them with a pattern.

import re
import json

data = """
link-aggregate 0 AdminState:Up OperState:IS
Member Ports: 6/1 6/2 6/3 6/4 6/5
6/6 6/7 6/8
7/1 7/2 /7/3
Description: << The port description >>
"""
#keys = ['link-aggregate', 'AdminState', 'OperState', 'Member Ports', 'Description']

p = re.compile(r'(link-aggregate) (\d+)') # get link-aggregate 0
q = re.compile(r'(AdminState):([A-Za-z]+)') # get AdminState Up
r = re.compile(r'(OperState):([A-Za-z]+)') # get OperState IS
s = re.compile(r'(Member Ports):') # get Member Ports
t = re.compile('([0-9]+/[0-9]+)') # get Member Ports values
u = re.compile('(Description:) <<([\s\S]*?)>>') # get Description

def getbits(data):
    d = {}
    resp = p.search(data) # res.group(2) = 0
    d[resp.group(1)] = resp.group(2)
    resq = q.search(data) # res.group(2) = 'Up'
    d[resq.group(1)] = resq.group(2)          
    resr = r.search(data) # res.group(2) = 'IS'
    d[resr.group(1)] = resr.group(2)
    # different because t uses .findall() not .search
    ress = s.search(data) # res.group(1) = 'Member Ports'   
    rest = t.findall(data) # res = ['6/1', '6/2', '6/3', '6/4', '6/5', '6/6', '6/7', '6/8', '7/1', '7/2', '7/3']
    d[ress.group(1)] = rest
    resu = u.search(data) # res.group(2) = ' The port description '
    d[resu.group(1)] = resu.group(2)
    return d

# if you have many sets of data, loop through them
# here there is only 1 data
data_dict = getbits(data)
for item in data_dict.items():
    print(item)

# save as json
mypath = '/home/pedro/temp/myjson.json'    
with open(mypath, 'a') as json_file:
    json.dump(data_dict, json_file)
    print('data saved to', mypath)
data_dict

Output:
for item in data_dict.items(): print(item) ('link-aggregate', '0') ('AdminState', 'Up') ('OperState', 'IS') ('Member Ports', ['6/1', '6/2', '6/3', '6/4', '6/5', '6/6', '6/7', '6/8', '7/1', '7/2', '7/3']) ('Description:', ' The port description ')
I never had much luck with re.VERBOSE and a complex, multiline re expression, so I just keep it simple!
Reply


Messages In This Thread
RE: Using TTP to extract an unknow number of words from a line - by Pedroski55 - May-25-2024, 05:57 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Can python detect style of language? eg. Flowery words vs simple words mcp111 4 2,629 Jan-07-2020, 02:25 PM
Last Post: mcp111

Forum Jump:

User Panel Messages

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