2

I am new to python and am trying to read a file and create a dictionary from it. The format is as follows:

.1.3.6.1.4.1.14823.1.1.27 {
    TYPE = Switch
    VENDOR = Aruba
    MODEL = ArubaS3500-48T
    CERTIFICATION = CERTIFIED
    CONT = Aruba-Switch
    HEALTH = ARUBA-Controller
    VLAN = Dot1q    INSTRUMENTATION:
     Card-Fault            = ArubaController:DeviceID
     CPU/Memory            = ArubaController:DeviceID
     Environment              = ArubaSysExt:DeviceID
     Interface-Fault       = MIB2
     Interface-Performance = MIB2
     Port-Fault            = MIB2
     Port-Performance      = MIB2 
}

The first line OID (.1.3.6.1.4.1.14823.1.1.27 { ) I want this to be the key and the remaining lines are the values until the }

I have tried a few combinations but am not able to get the correct regex to match these

Any help please?

I have tried something like

lines = cache.readlines()

for line in lines:

    searchObj = re.search(r'(^.\d.*{)(.*)$', line)

    if searchObj:
        (oid, cert ) = searchObj.groups()

    results[searchObj(oid)] = ", ".join(line[1:])

    print("searchObj.group() : ", searchObj.group(1))

    print("searchObj.group(1) : ", searchObj.group(2))
1
  • yes its always .1.3.6.1.4.x.x.x.x Commented Oct 17, 2017 at 21:01

2 Answers 2

1

You can try this:

import re
data = open('filename.txt').read()
the_key = re.findall("^\n*[\.\d]+", data)
values = [re.split("\s+\=\s+", i) for i in re.findall("[a-zA-Z0-9]+\s*\=\s*[a-zA-Z0-9]+", data)]
final_data = {the_key[0]:dict(values)}

Output:

{'\n.1.3.6.1.4.1.14823.1.1.27': {'VENDOR': 'Aruba', 'CERTIFICATION': 'CERTIFIED', 'Fault': 'MIB2', 'VLAN': 'Dot1q', 'Environment': 'ArubaSysExt', 'HEALTH': 'ARUBA', 'Memory': 'ArubaController', 'Performance': 'MIB2', 'CONT': 'Aruba', 'MODEL': 'ArubaS3500', 'TYPE': 'Switch'}}
Sign up to request clarification or add additional context in comments.

Comments

1

You could use a nested dict comprehension along with an outer and inner regex.


Your blocks can be separated by

.numbers...numbers.. {
    // values here
}

In terms of regular expression this can be formulated as

^\s*                 # start of line + whitespaces, eventually
(?P<key>\.[\d.]+)\s* # the key
{(?P<values>[^{}]+)} # everything between { and }

As you see, we split the parts into key/value pairs.


Your "inner" structure can be formulated like

(?P<key>\b[A-Z][-/\w]+\b) # the "inner" key
\s*=\s*                   # whitespaces, =, whitespaces
(?P<value>.+)             # the value


Now let's build the "outer" and "inner" expressions together:

rx_outer = re.compile(r'^\s*(?P<key>\.[\d.]+)\s*{(?P<values>[^{}]+)}', re.MULTILINE)
rx_inner = re.compile(r'(?P<key>\b[A-Z][-/\w]+\b)\s*=\s*(?P<value>.+)')

result = {item.group('key'): 
    {match.group('key'): match.group('value') 
    for match in rx_inner.finditer(item.group('values'))} 
    for item in rx_outer.finditer(string)}
print(result)

A demo can be found on ideone.com.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.