处理memory output

def parse(content):
    ram_dict={}
    key_map={          #转换成我们需要的名字
        'Size': 'capacity',
        'Locator': 'slot',
        'Type': 'model',
        'Speed': 'speed',
        'Manufacturer': 'manufacturer',
        'Serial Number': 'sn',
    }
    devices=content.split("Memory Device")
    for item in devices:
        item=item.strip()
        if not item:
            continue
        if item.startswith("#"):
            continue
        segment={}
        lines=item.split('
	')
        for line in lines:
            if len(line.split(":")) >1:
                key,value=line.split(":")
            else:
                key=line.split(":")[0]
                value=""
            if key in key_map:
                segment[key_map[key]]=value.strip()        #生成字典
        ram_dict[segment['slot']]=segment       #提出槽位最为key
    return ram_dict
content=open(r'F:autocsplitmemory.out','r').read()
res=parse(content)
print(res)
{'DIMM #0': {'capacity': '1024 MB', 'slot': 'DIMM #0', 'model': 'DRAM', 'speed': '667 MHz', 'manufacturer': 'Not Specified', 'sn': 'Not Specified'},
'DIMM #1': {'capacity': 'No Module Installed', 'slot': 'DIMM #1', 'model': 'DRAM', 'speed': '667 MHz', 'manufacturer': 'Not Specified', 'sn': 'Not Specified'},
'DIMM #2': {'capacity': 'No Module Installed', 'slot': 'DIMM #2', 'model': 'DRAM', 'speed': '667 MHz', 'manufacturer': 'Not Specified', 'sn': 'Not Specified'},

'DIMM #3': {'capacity': 'No Module Installed', 'slot': 'DIMM #3', 'model': 'DRAM', 'speed': '667 MHz', 'manufacturer': 'Not Specified', 'sn': 'Not Specified'},

'DIMM #4': {'capacity': 'No Module Installed', 'slot': 'DIMM #4', 'model': 'DRAM', 'speed': '667 MHz', 'manufacturer': 'Not Specified', 'sn': 'Not Specified'},

'DIMM #5': {'capacity': 'No Module Installed', 'slot': 'DIMM #5', 'model': 'DRAM', 'speed': '667 MHz', 'manufacturer': 'Not Specified', 'sn': 'Not Specified'},

'DIMM #6': {'capacity': 'No Module Installed', 'slot': 'DIMM #6', 'model': 'DRAM', 'speed': '667 MHz', 'manufacturer': 'Not Specified', 'sn': 'Not Specified'},

'DIMM #7': {'capacity': 'No Module Installed', 'slot': 'DIMM #7', 'model': 'DRAM', 'speed': '667 MHz', 'manufacturer': 'Not Specified', 'sn': 'Not Specified'}}
Memory Device
    Total Width: 32 bits
    Data Width: 32 bits
    Size: 1024 MB
    Form Factor: DIMM
    Set: None
    Locator: DIMM #0
    Bank Locator: BANK #0
    Type: DRAM
    Type Detail: EDO
    Speed: 667 MHz
    Manufacturer: Not Specified
    Serial Number: Not Specified
    Asset Tag: Not Specified
    Part Number: Not Specified
    Rank: Unknown

Memory Device
    Total Width: 32 bits
    Data Width: 32 bits
    Size: No Module Installed
    Form Factor: DIMM
    Set: None
    Locator: DIMM #1
    Bank Locator: BANK #1
    Type: DRAM
    Type Detail: EDO
    Speed: 667 MHz
    Manufacturer: Not Specified
    Serial Number: Not Specified
    Asset Tag: Not Specified
    Part Number: Not Specified
    Rank: Unknown

Memory Device
    Total Width: 32 bits
    Data Width: 32 bits
    Size: No Module Installed
    Form Factor: DIMM
    Set: None
    Locator: DIMM #2
    Bank Locator: BANK #2
    Type: DRAM
    Type Detail: EDO
    Speed: 667 MHz
    Manufacturer: Not Specified
    Serial Number: Not Specified
    Asset Tag: Not Specified
    Part Number: Not Specified
    Rank: Unknown

Memory Device
    Total Width: 32 bits
    Data Width: 32 bits
    Size: No Module Installed
    Form Factor: DIMM
    Set: None
    Locator: DIMM #3
    Bank Locator: BANK #3
    Type: DRAM
    Type Detail: EDO
    Speed: 667 MHz
    Manufacturer: Not Specified
    Serial Number: Not Specified
    Asset Tag: Not Specified
    Part Number: Not Specified
    Rank: Unknown

Memory Device
    Total Width: 32 bits
    Data Width: 32 bits
    Size: No Module Installed
    Form Factor: DIMM
    Set: None
    Locator: DIMM #4
    Bank Locator: BANK #4
    Type: DRAM
    Type Detail: EDO
    Speed: 667 MHz
    Manufacturer: Not Specified
    Serial Number: Not Specified
    Asset Tag: Not Specified
    Part Number: Not Specified
    Rank: Unknown

Memory Device
    Total Width: 32 bits
    Data Width: 32 bits
    Size: No Module Installed
    Form Factor: DIMM
    Set: None
    Locator: DIMM #5
    Bank Locator: BANK #5
    Type: DRAM
    Type Detail: EDO
    Speed: 667 MHz
    Manufacturer: Not Specified
    Serial Number: Not Specified
    Asset Tag: Not Specified
    Part Number: Not Specified
    Rank: Unknown

Memory Device
    Total Width: 32 bits
    Data Width: 32 bits
    Size: No Module Installed
    Form Factor: DIMM
    Set: None
    Locator: DIMM #6
    Bank Locator: BANK #6
    Type: DRAM
    Type Detail: EDO
    Speed: 667 MHz
    Manufacturer: Not Specified
    Serial Number: Not Specified
    Asset Tag: Not Specified
    Part Number: Not Specified
    Rank: Unknown

Memory Device
    Total Width: 32 bits
    Data Width: 32 bits
    Size: No Module Installed
    Form Factor: DIMM
    Set: None
    Locator: DIMM #7
    Bank Locator: BANK #7
    Type: DRAM
    Type Detail: EDO
    Speed: 667 MHz
    Manufacturer: Not Specified
    Serial Number: Not Specified
    Asset Tag: Not Specified
    Part Number: Not Specified
    Rank: Unknown
原文地址:https://www.cnblogs.com/wuxi9864/p/10103530.html