使用argparse处理不确定的成对参数 [英] Handling indefinite paired arguments with argparse

查看:306
本文介绍了使用argparse处理不确定的成对参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中,我需要定义类似

mcraw recipe add COUNT ID COUNT_1 ID_1 [COUNT_2 ID_2 ..]

argparse 似乎是一般工作的最佳工具

and argparse seems to be the best tool for the general job.

如何指示Python及其argparse构造这样的字典?

How can I instruct Python and its argparse to construct a dictionary like this?

{
  ID_1: COUNT_1,
  ID_2: COUNT_2,
  ...
}

推荐答案

成对读取参数:

argdict = {args[i + 1]: args[i] for i in xrange(0, len(args), 2)}

argparse对此输入没有特殊处理.

argparse has otherwise no special handling for this kind of input.

这篇关于使用argparse处理不确定的成对参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆