0
我在我的代碼中定義了一個名爲cfg
的字典。試圖用另一個「字典」更新字典
cfg = { 'debug': 1, 'verbose': 1, 'cfgfile': 'my.cfg' }
使用ConfigParser
我分析可用於覆蓋在上面cfg
定義的硬編碼值和如下合併它們的配置文件:
config = SafeConfigParser()
config.read(cfg['cfgfile'])
cfg.update(dict(config.items('Main')))
上述所有工作正常。
我現在調用一個函數,它使用optparse
來解析命令行參數。
def parseOptions():
parser = OptionParser()
parser.add_option("-d", "", dest="debug", action="store_true", default=False, help="enable additional debugging output")
parser.add_option("-v", "", dest="verbose", action="store_true", default=False, help="enable verbose console output")
(options, args) = parser.parse_args()
return options
早在main()
,options
似乎是在目測時的字典:
options = parseOptions()
print options
{'debug': False, 'verbose': False}
當我嘗試更新我的cfg
字典,我得到這個錯誤:
cfg.update(dict(options))
輸出:
Traceback (most recent call last):
File "./myscript.py", line 176, in <module>
cfg.update(dict(options))
TypeError: iteration over non-sequence
類型的選項是價值觀的一個實例:
print "type(options)=%s instanceof=%s\n" % (type(options), options.__class__.__name__)
type(options)=<type 'instance'> instanceof=Values
我如何更新我的cfg
字典,在options
值是多少?
真棒,就像一個魅力! – BenH 2014-10-16 13:36:03