gpt4 book ai didi

python - 如何将 python 字典复制到二维字典中?

转载 作者:太空宇宙 更新时间:2023-11-04 05:46:12 29 4
gpt4 key购买 nike

我正在逐行读取包含系统信息的文件,当我看到等号时,将其转换为标准字典中的键:值条目,直到到达结束单个系统信息的行。然后我想将该字典复制到二维字典中 - 即 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})

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

最佳答案

我不明白你到底想做什么。

但是,如果你想做这样的事情:

sysarray = {
'file1': {
'key1': 'value1',
'key2': 'value2'
},
'file2': {
'key1': 'value1',
'key2': 'value2'
}
}

您可以更改最后一行:

if "Install" in rline:
list(entryarr)
for ky in entryarr.keys():
sysarray[entryarr[Name]] = { ky = entryarr[ky]r})

到:

if "Install" in rline:
sysarray[filename] = entryarr

关于python - 如何将 python 字典复制到二维字典中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32188954/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com