python argparse传入布尔参数false不生效的解决


Posted in Python onApril 20, 2020

跑代码时,在命令行给python程序传入bool参数,但无法传入False,无论传入True还是False,程序里面都是True。下面是代码:

parser.add_argument("--preprocess", type=bool, default=True, help='run prepare_data or not')

高端解决方案

使用可选参数store_true,将上述代码改为:

parse.add_argument("--preprocess", action='store_true', help='run prepare_data or not')

在命令行执行py文件时,不加--preprocess,默认传入的preprocess参数为False;

如果加--preprocess,则传入的是True。

还可以将上述代码改为:

parse.add_argument("--preprocess", default='False', action='store_true', help='run prepare_data or not')

和 1 中表达的意思完全相同。

在命令行执行py文件时,不加--preprocess,默认传入的preprocess参数为False;

如果加--preprocess,则传入的是True。

还可以将上述代码改为:

parse.add_argument("--preprocess", default='True', action='store_true', help='run prepare_data or not')

和 1 中表达的意思完全相反。

在命令行执行py文件时,不加--preprocess,默认传入的preprocess参数为True;

如果加--preprocess,则传入的是False。

产生的原因和较Low的解决方案

猜测可能的原因是数据类型导致的,传入的都是string类型,转为bool型时,由于是非空字符串,所以转为True。

从这个角度去更改的话,由于type参数接收的是callable的参数类型来对我们接收的原始参数做处理,我们可以定义一个函数赋值给type参数,用它对原始参数做处理:

parser.add_argument("--preprocess", type=str2bool, default='True', help='run prepare_data or not')

下面定义这个函数将str类型转换为bool型:

def str2bool(str):
return True if str.lower() == 'true' else False

补充知识:parser.add_argument验证格式

我就废话不多说了,还是直接看代码吧!

article_bp = Blueprint('article', __name__, url_prefix='/api')

api = Api(article_bp)
parser = reqparse.RequestParser()
parser.add_argument('name', type=str, help='必须填写名称', required=True)
channel_fields = {
 'id': fields.Integer,
 'cname': fields.String
}

class ChannelResource(Resource):
 def get(self):
 channels = Channel.query.all()
 return marshal(channels, channel_fields)

 def post(self):
 args = parser.parse_args()
 if args:
  channel = Channel()
  channel.cname = args.get('name')
  channel.save()
  return {'msg': '频道添加成功', 'channel': marshal(channel, channel_fields)}
 else:
  return {'msg': '频道添加失败'}

以上这篇python argparse传入布尔参数false不生效的解决就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python实现的Kmeans++算法实例
Apr 26 Python
一步步教你用Python实现2048小游戏
Jan 19 Python
详解Python读取配置文件模块ConfigParser
May 11 Python
Python装饰器实现几类验证功能做法实例
May 18 Python
Python中三元表达式的几种写法介绍
Mar 04 Python
Python OpenCV利用笔记本摄像头实现人脸检测
Aug 20 Python
python pillow模块使用方法详解
Aug 30 Python
利用rest framework搭建Django API过程解析
Aug 31 Python
Python爬虫图片懒加载技术 selenium和PhantomJS解析
Sep 18 Python
django中url映射规则和服务端响应顺序的实现
Apr 02 Python
python tqdm库的使用
Nov 30 Python
详解使用scrapy进行模拟登陆三种方式
Feb 21 Python
parser.add_argument中的action使用
Apr 20 #Python
Python ArgumentParse的subparser用法说明
Apr 20 #Python
python列表的逆序遍历实现
Apr 20 #Python
python sitk.show()与imageJ结合使用常见的问题
Apr 20 #Python
使用Python对Dicom文件进行读取与写入的实现
Apr 20 #Python
python 错误处理 assert详解
Apr 20 #Python
解决Jupyter Notebook使用parser.parse_args出现错误问题
Apr 20 #Python
You might like
thinkphp自定义权限管理之名称判断方法
2017/04/01 PHP
取得一定长度的内容,处理中文
2006/12/20 Javascript
关于JavaScript中的关联数组分析
2013/04/09 Javascript
js出生日期 年月日级联菜单示例代码
2014/01/10 Javascript
jQuery:delegate中select()不起作用的解决方法(实例讲解)
2014/01/26 Javascript
基于JavaScript实现定时跳转到指定页面
2016/01/01 Javascript
jQuery轮播图效果精简版完整示例
2016/09/04 Javascript
jQuery插入节点和移动节点用法示例(insertAfter、insertBefore方法)
2016/09/08 Javascript
页面缩放兼容性处理方法(zoom,Firefox火狐浏览器)
2017/08/29 Javascript
基于iScroll实现内容滚动效果
2018/03/21 Javascript
解决layer弹出层自适应页面大小的问题
2019/09/16 Javascript
JS实现放大镜效果
2020/09/21 Javascript
[06:48]DOTA2-DPC中国联赛2月26日Recap集锦
2021/03/11 DOTA
python友情链接检查方法
2015/07/08 Python
利用Python实现Windows定时关机功能
2017/03/21 Python
Django使用Mysql数据库已经存在的数据表方法
2018/05/27 Python
Python网络爬虫之爬取微博热搜
2019/04/18 Python
PyTorch搭建一维线性回归模型(二)
2019/05/22 Python
Python提取转移文件夹内所有.jpg文件并查看每一帧的方法
2019/06/27 Python
使用python模拟高斯分布例子
2019/12/09 Python
Python将列表中的元素转化为数字并排序的示例
2019/12/25 Python
tensorflow-gpu安装的常见问题及解决方案
2020/01/20 Python
Django3中的自定义用户模型实例详解
2020/08/23 Python
css3截图_动力节点Java学院整理
2017/07/11 HTML / CSS
加拿大女包品牌:Matt & Nat
2017/05/12 全球购物
BrandAlley英国:法国折扣奢侈品网上零售商
2017/07/03 全球购物
创业计划书六个要素
2013/12/26 职场文书
大学四年规划书范文
2013/12/27 职场文书
《祁黄羊》教学反思
2014/04/22 职场文书
三好学生演讲稿范文
2014/04/26 职场文书
任命书怎么写
2014/06/04 职场文书
建筑工程造价专业自荐信
2014/07/08 职场文书
公务员年终个人总结
2015/02/12 职场文书
民间借贷被告代理词
2015/05/23 职场文书
标准演讲稿格式结尾应该怎么书写?
2019/07/17 职场文书
vue实力踩坑之push当前页无效
2022/04/10 Vue.js