Python optparse 值实例
我如何获取 opt 结果
opt, args = parser.parse_args()
并将其放入字典中? Python 将 opt 称为“值实例”,但我找不到任何方法将值实例转换为列表或字典。人们不能以这种方式从 opt 复制项目,
for i in opt:
myDict[i] = opt[i]
相反,它很笨拙,
myDict[parm1] = opt.parm1
myDict[parm2] = opt.parm2
这意味着每次我添加一个选项时,我也必须更新此代码;应该有办法让它自行解决。
How can I take the opt result of
opt, args = parser.parse_args()
and place it in a dict? Python calls opt a "Values Instance" and I can't find any way to turn a Values Instance into a list or dict. One can't copy items from opt in this way,
for i in opt:
myDict[i] = opt[i]
instead, its a clumsy,
myDict[parm1] = opt.parm1
myDict[parm2] = opt.parm2
which implies that every time I add an option, I have to update this code as well; there should be a way to let this take care of itself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(来源是这篇 python-ideas 帖子。)
(Source is this python-ideas post.)