有一个奇怪的时间让这个工作;
我有一个包含多台机器信息的字典。根据参数,选择机器。
我想将所选信息写入日志。但我的递归尝试似乎并没有让我到达我需要去的地方。我得到了密钥,但价值失败了。
这是字典
CSTU_CFG = {'A07': {
'password': 'CastAIP', # default cast password( too lazy to use LDAP)
'host':'JSIPVWCASTD01',
'port':'2280', # Ports are assumed to be 2280 but can be any
'location': 'C:Users/WDI/Documents/CSTU/DMPRST', # use os.path to convert
'gzips': 'GZIPS', # location for zip files ( Backup )
'schematype':{'local', 'central', 'mngt'},
'logintv': 30,
'version': '0.9'
},
'A01': {
'machine': 'A01',
'password': 'CastAIP', # default cast password( too lazy to use LDAP)
'host':'JSIPVWCASTD01',
'port':'2280', # Ports are assumed to be 2280 but can be any
'location': 'C:Users/WDI/Documents/CSTU/DMPRST', # use os.path to convert
'gzips': 'GZIPS', # location for zip files ( Backup )
'schematype':{'local', 'central', 'mngt'},
'logintv': 30,
'version': '0.9'
},
'A02': {
'machine': 'A02',
'password': 'CastAIP', # default cast password( too lazy to use LDAP)
'host':'JSIPVWCASTD01',
'port':'2280', # Ports are assumed to be 2280 but can be any
'location': 'C:Users/WDI/Documents/CSTU/DMPRST', # use os.path to convert
'gzips': 'GZIPS', # location for zip files ( Backup )
'schematype':{'local', 'central', 'mngt'},
'logintv': 30,
'version': '0.9'
}
}
logname = 'CSTU_'+timestr+'_'+ schemaname + '.CLOG'
logging.basicConfig(filename=logname,filemode='a',format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s', datefmt='%H:%M:%S',level=logging.DEBUG)
logging.debug("Starting CSTU_DUMP")
# print the CSTU_CFG file into the log
for key,value in CSTU_CFG:
logging.debug(key + " => " + value)
我显然没有明白日志。我已经尝试了一些建议的修复程序,但我得不到任何结果,或者出现各种错误。我可以很难对它进行硬编码,但这不是意图。 感谢
答案 0 :(得分:0)
要迭代字典项,您需要先访问这些项,而不是直接迭代字典。
您需要更改代码(for key,value in CSTU_CFG
),如下所示。
对于Python 2.x:
for key, value in CSTU_CFG.iteritems():
logging.debug(key + " => " + str(value))
对于Python 3.x:
for key, value in CSTU_CFG.items():
logging.debug(key + " => " + str(value))
顺便说一下,你说你得到了错误。下次在您的问题中包含确切的错误跟踪可能会有所帮助。
答案 1 :(得分:0)
您可以迭代字典以获取密钥和该密钥的值:
for k, v in CSTU_CFG.iteritems():
logging.debug(k, v)