Python 中argparse模块的使用


正文共495个字,预计阅读时间5分钟。


Python解析命令行读取参数有两种方式:sys.argv和argparse


1、sys.argv


如果脚本很简单或临时使用,没有多个复杂的参数选项,可以直接利用sys.argv将脚本后的参数依次读取(读进来的默认是字符串格式)。


1import sys
2print("输入的参数为:%s" % sys.argv[1])


命令行执行效果:


1>python demo.py 1
2输入的参数为:1


2、argparse


如果参数很多,比较复杂,并且类型不统一,那么argparse可以很好的解决这些问题,下面一个实例解释了argparse的基本使用方法。


 1import argparse
2# description参数可以用于描述脚本的参数作用,默认为空
3parser=argparse.ArgumentParser(description="A description of what the program does")
4parser.add_argument('--toy','-t',action='store_true',help='Use only 50K samples of data')
5 parser.add_argument('--num_epochs',choices=[5,10,20],default=5,type=int,help='Number of epochs.')
6parser.add_argument("--num_layers"type=int, required=True, help="Network depth.")
7
8args=parser.parse_args()
9print(args)
10print(args.toy,args.num_epochs,args.num_layers)


命令行执行效果:


1>python demo.py --num_epochs 10 --num_layers 10
2Namespace(num_epochs=10, num_layers=10, toy=False)
3False 10 10


2.1.基本使用

1parser.add_argument('--toy','-t',action='store_true',help='Use only 50K samples of data')


--toy:为参数名称;
-t:为参数别称;
action='store_true':参数是否使用,如果使用则为True,否则为False。


1>python demo.py -t --num_epochs 10 --num_layers 10
2Namespace(num_epochs=10, num_layers=10, toy=True)
3True 10 10 # 对比和上次执行的区别


help:参数说明


2.2.相关参数

实例1


1parser.add_argument('--num_epochs',choices=[5,10,20],default=5,type=int,help='Number of epochs.')


choices:候选值,输出参数必须在候选值里面,否如会出现下面的结果:


1>python demo.py -t --num_epochs 30 --num_layers 10
2usagedemo.py [-h] [--toy] [--num_epochs {5,10,20}] --num_layers NUM_LAYERS
3demo.pyerrorargument --num_epochsinvalid choice: 30 (choose from 5, 10, 20)


default:默认值,如果不输入参数,则使用该默认值


1>python demo.py -t  --num_layers 10
2Namespace(num_epochs=5, num_layers=10, toy=True)
3True 5 10


int:参数类型


实例2


1parser.add_argument("--num_layers"type=int, required=True, help="Network depth.")


required:为必选参数,如果不输入,则出现以下错误:


1>python demo.py -t --num_epochs 10
2usagedemo.py [-h] [--toy] [--num_epochs {5,10,20}] --num_layers NUM_LAYERS
3demo.pyerrorthe following arguments are required--num_layers


实例3


-h:输出参数使用说明信息


 1>python demo.py -h
2usage: demo.py [-h] [--toy] [--num_epochs {5,10,20}] --num_layers NUM_LAYERS
3
4A description of what the program does
5
6optional arguments:
7-h, --help            show this help message and exit
8--toy, -t             Use only 50K samples of data
9--num_epochs {5,10,20}
10                Number of epochs.
11 --num_layers NUM_LAYERS
12                Network depth.


原文链接:https://www.jianshu.com/p/8f781143c524


查阅更为简洁方便的分类文章以及最新的课程、产品信息,请移步至全新呈现的“LeadAI学院官网”:

www.leadai.org


请关注人工智能LeadAI公众号,查看更多专业文章

640?wx_fmt=jpeg

大家都在看

640.png?

LSTM模型在问答系统中的应用

基于TensorFlow的神经网络解决用户流失概览问题

最全常见算法工程师面试题目整理(一)

最全常见算法工程师面试题目整理(二)

TensorFlow从1到2 | 第三章 深度学习革命的开端:卷积神经网络

装饰器 | Python高级编程

今天不如来复习下Python基础

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值