从文件python创建字典

时间:2021-11-29 20:27:30

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

我是python新手,正在尝试读取一个文件并从中创建一个字典。格式如下:

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

第一行OID (. 1.3.6.1.1.14823.1.1.27 {)

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

我尝试了一些组合,但无法获得与这些匹配的正确regex

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

2 个解决方案

#1


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

#2


1  

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

您可以使用嵌套的字典理解以及外部和内部的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.

演示可以在ideone.com上找到。

#1


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

#2


1  

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

您可以使用嵌套的字典理解以及外部和内部的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.

演示可以在ideone.com上找到。