如何将python字典复制到2d字典中?

时间:2015-08-24 18:12:52

标签: python dictionary

我正在逐行阅读带有系统信息的文件,当我看到一个等号时,将其转换为标准字典中的一个键:值条目,直到我到达一条结束单个系统信息的行。 然后,我想将该字典复制到一个二维字典中 - 即dict2 [dict1 [name]] = {dict1.key = dict1.value}迭代键,然后对下一个系统重复此操作。

我在2d设置上一直遇到语法错误:

以下是示例输入:

Host is linux4493

Name = linux4493
Class = Linux
Family = Hardware.Server
Host Name = linux4493
Serial Number = KQ72BWY
MAC Address = 34:40:b5:aa:24:14
DNS Name = ;;
EIT Managed? =
BU =
Location =
1. CMDB Attributes
  1. Attributes
                Memory Installed = 72GB
                Disk Capacity = 1394.18GB
                Processor Type = Xeon
                Processor Cache = 12MB
                Processor Speed =  2.67GHz
                Processor Sockets = 2
                Number of Physical Processors Installed = 2
                Number of Cores per Processor = 6
                Number of Memory Slots = 18
                OS Version = RHEL6
                Primary Application = InfraRed5

  6. Inventory
                IP Address = 192.168.1.41
                Install Date = 09/28/2012 12:55 PM

主机是rh6u3x64mt

TOP OF FORM
Name = rh6u3x64mt
Class = Linux_VM
Family = Hardware.Virtual Machine
Host Name = rh6u3x64mt
Serial Number = VMware-42 00 bf 4c 39 d0 29 92-a5 dd 45 02 a4 d7 a2 79
Virtual MAC Address = 00:50:56:80:00:99
Virtual Host Name = rh6u3x64mt.test.com
EIT Managed? =
BU =
Location =

1. CMDB Attributes
        1. Attributes
                Memory Installed = 16GB
                Disk Capacity = 4336.00GB
                Processor Type = Xeon
                Processor Speed =  2.00GHz
                Number of Virtual Processors = 2
                Memory Minimum = 2GB
                Memory Allocated = 16GB
                OS Version = RHEL6
                Primary Application = Metrotech
                Cluster Name =

        6. Inventory
                IP Address = 192.168.8.110
                Installation Date = 09/27/2012 10:45 AM

以下是代码:

import os, getopt, sys, fileinput
from array import *

sysarray = {}
entryarr = {}

for rline in fileinput.input():
    if "=" in rline:
        (rlkey, rlval) = rline.split("=")
        if rlkey.strip() == "Virtual Host Name":
            rlkey = "DNS Name"
        entryarr[rlkey.strip()] = rlval.strip()
        if "Install" in rline:
            list(entryarr)
            for ky in entryarr.keys():
                sysarray[entryarr[Name]] = { ky = entryarr[ky]r})

我似乎无法确定最后一行的正确语法。 有什么建议吗?

0 个答案:

没有答案