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中类的继承代码实例
Oct 28 Python
将Django使用的数据库从MySQL迁移到PostgreSQL的教程
Apr 11 Python
Python实现简单字典树的方法
Apr 29 Python
Python 爬虫多线程详解及实例代码
Oct 08 Python
Python使用flask框架操作sqlite3的两种方式
Jan 31 Python
用Python读取几十万行文本数据
Dec 24 Python
Python字符串对象实现原理详解
Jul 01 Python
python中selenium操作下拉滚动条的几种方法汇总
Jul 14 Python
Python小白学习爬虫常用请求报头
Jun 03 Python
Python3.9 beta2版本发布了,看看这7个新的PEP都是什么
Jun 10 Python
conda安装tensorflow和conda常用命令小结
Feb 20 Python
Python matplotlib绘制条形统计图 处理多个实验多组观测值
Apr 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
实用函数2
2007/11/08 PHP
PHP中的MYSQL常用函数(php下操作数据库必备)
2010/09/12 PHP
php正则过滤html标签、空格、换行符的代码(附说明)
2010/10/25 PHP
php错误提示failed to open stream: HTTP request failed!的完美解决方法
2011/06/06 PHP
浅析虚拟主机服务器php fsockopen函数被禁用的解决办法
2013/08/07 PHP
PHP加密扩展库Mcrypt安装和实例
2013/11/10 PHP
浅析get与post的一些特殊情况
2014/07/28 PHP
php过滤表单提交的html等危险代码
2014/11/03 PHP
CodeIgniter使用smtp服务发送html邮件的方法
2015/06/10 PHP
php面试中关于面向对象的相关问题
2019/02/13 PHP
Yii框架的路由配置方法分析
2019/09/09 PHP
鼠标滚轮改变图片大小的示例代码
2013/11/20 Javascript
ExtJS 刷新后如何默认选中刷新前最后一次选中的节点
2014/04/03 Javascript
jQuery跨域问题解决方案
2015/08/03 Javascript
jQuery实现横向带缓冲的水平运动效果(附demo源码下载)
2016/01/29 Javascript
JavaScript中push(),join() 函数 实例详解
2016/09/06 Javascript
ES6新特性六:promise对象实例详解
2017/04/21 Javascript
javaScript之split与join的区别(详解)
2017/11/08 Javascript
JavaScript如何处理移动端拍摄图片旋转问题
2019/11/16 Javascript
[08:54]DOTA2-DPC中国联赛 正赛 Aster vs LBZS 选手采访
2021/03/11 DOTA
python使用wxpython开发简单记事本的方法
2015/05/20 Python
在Python中通过threading模块定义和调用线程的方法
2016/07/12 Python
python+selenium开发环境搭建图文教程
2017/08/11 Python
使用实现XlsxWriter创建Excel文件并编辑
2018/05/04 Python
在Python中输入一个以空格为间隔的数组方法
2018/11/13 Python
python3+PyQt5 数据库编程--增删改实例
2019/06/17 Python
python3实现带多张图片、附件的邮件发送
2019/08/10 Python
Django框架教程之中间件MiddleWare浅析
2019/12/29 Python
python对批量WAV音频进行等长分割的方法实现
2020/09/25 Python
Pycharm 解决自动格式化冲突的设置操作
2021/01/15 Python
计算机专业个人求职自荐信
2013/09/21 职场文书
求职信写作要突出重点
2014/01/01 职场文书
民主评议政风行风整改方案
2014/09/17 职场文书
计算机专业自荐信
2015/03/05 职场文书
Spring Cloud 中@FeignClient注解中的contextId属性详解
2021/09/25 Java/Android
Oracle 11g数据库使用expdp每周进行数据备份并上传到备份服务器
2022/06/28 Oracle