简体   繁体   中英

create a dictionary from file python

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))

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'}}

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


Your blocks can be separated by

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

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

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)


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[AZ][-/\\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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM