我想使用argparse来解析写为"--foo True"或"--foo False"的布尔命令行参数.例如:
my_program --my_boolean_flag False
Run Code Online (Sandbox Code Playgroud)
但是,以下测试代码不能满足我的要求:
import argparse
parser = argparse.ArgumentParser(description="My parser")
parser.add_argument("--my_bool", type=bool)
cmd_line = ["--my_bool", "False"]
parsed_args = parser.parse(cmd_line)
Run Code Online (Sandbox Code Playgroud)
可悲的是,parsed_args.my_bool评估为True.这种情况即使我改变cmd_line为["--my_bool", ""],这是令人惊讶的,因为bool("")重新评估False.
如何让argparse解析"False","F"以及它们的小写变体False?
我必须在包含10-100k这些元素的列表中检查数百万个元素(20-30个字母str)的存在.在python中有更快的方法set()吗?
import sys
#load ids
ids = set( x.strip() for x in open(idfile) )
for line in sys.stdin:
id=line.strip()
if id in ids:
#print fastq
print id
#update ids
ids.remove( id )
Run Code Online (Sandbox Code Playgroud) 我正在寻找将Python中的所有非数字数据(包括空格)转换为零的最简单方法.以下面的例子为例:
someData = [[1.0,4,'7',-50],['8 bananas','text','',12.5644]]
Run Code Online (Sandbox Code Playgroud)
我想输出如下:
desiredData = [[1.0,4,7,-50],[0,0,0,12.5644]]
Run Code Online (Sandbox Code Playgroud)
所以'7'应该是7,但'8香蕉'应该转换为0.
我正在尝试将字符串 'TRUE' 和 'FALSE' 的数据框中的列转换为布尔值。对于其他数据类型,我只是使用 astype 并且没有任何问题,但出于某种原因,在此列上使用它会将所有值转换为 True。
我对字符串列表重复了测试如下
test = ['False','False','True']
test = pd.DataFrame(test)
test = test.astype('bool')
Run Code Online (Sandbox Code Playgroud)
但它给出了相同的结果,这里发生了什么以及如何正确转换数据类型?我试过使用 map 和 replace 在转换之前更改值,但都没有改变结果。
型号代码:
class Task(db.Model):
complete = db.BooleanProperty(default=False)
Run Code Online (Sandbox Code Playgroud)
HTML代码:
<input type="checkbox" name="complete" value="True" />
Run Code Online (Sandbox Code Playgroud)
数据库:
task = Task()
task.complete = self.request.get('complete')
task.put()
Run Code Online (Sandbox Code Playgroud)
这会返回一个错误:
BadValueError:属性完整必须是bool
该怎么做?
我是python的新手.
我希望程序问
"is Johnny hungry? True or false?"
Run Code Online (Sandbox Code Playgroud)
用户输入True
然后打印"Johnny needs to eat."
用户输入false
然后打印"Johnny is full."
我知道要添加一个int我输入
johnnyHungry = int(input("Is johnny hungry "))
Run Code Online (Sandbox Code Playgroud)
但我希望他们输入True/false,而不是int.
我正在尝试将表示JSON对象的字符串转换为真正的JSON对象,json.loads但它不会转换整数:
(在初始字符串中,整数总是字符串)
$> python
Python 2.7.9 (default, Aug 29 2016, 16:00:38)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> c = '{"value": "42"}'
>>> json_object = json.loads(c, parse_int=int)
>>> json_object
{u'value': u'42'}
>>> json_object['value']
u'42'
>>>
Run Code Online (Sandbox Code Playgroud)
而不是{u'value': u'42'}我喜欢它成为{u'value': 42}.我知道我可以浏览整个对象,但我不想这样做,手动执行它并不是很有效,因为这个parse_int参数存在(https://docs.python.org/2/library/json. html#json.loads).
感谢皮尔斯的主张:
Python 2.7.9 (default, Aug 29 2016, 16:00:38)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on …Run Code Online (Sandbox Code Playgroud) 如何"True"用布尔值True或替换1?
mylist = ["Saturday", "True"]
Run Code Online (Sandbox Code Playgroud)
我试过更换,但得到错误:
TypeError: replace() argument 2 must be str, not bool
Run Code Online (Sandbox Code Playgroud)
提前致谢!
我正在尝试让用户输入他们是否喜欢辛辣食物,并且输出应该是布尔值,但是我的以下代码似乎没有输出:
def likes_spicyfood():
spicyfood = bool(input("Do you like spicy food? True or False?"))
if spicyfood == ("True"):
print("True")
if spicyfood == ("False"):
print("False")
return(likes_spicyfood)
Run Code Online (Sandbox Code Playgroud) 我试图找出如何将布尔值False从命令行传递到argparser。我的原始代码如下:
import argparse
parser = argparse.ArgumentParser(allow_abbrev=True)
parser.add_argument('-data', default=True, type=bool, help='uses the history file')
args = parser.parse_args(sys.argv[1:])
Run Code Online (Sandbox Code Playgroud)
在命令行中,我输入:python myscript.py -data False
在False周围也有单引号和双引号的变体。当我检查args命名空间的内容时,args.data始终为True。
因此,我将参数定义从bool更改为str,并使用默认的字符串“ True”,如下所示:
parser.add_argument('-data', default="True", type=str, help='uses the history file')
Run Code Online (Sandbox Code Playgroud)
然后,我对args进行了一些按摩,以获得我真正想要的布尔值:
if re.search("f", args.data, re.I):
args.data = False
else:
args.data = True
Run Code Online (Sandbox Code Playgroud)
此替代方法确实有效。有一个更好的方法吗?
我正在从配置为Input的通用IO(GPIO)中读取一个值,它返回一个0或1的字符串.我看到两种简单的方法将它转换为boolean:
bool(int(input_value))
Run Code Online (Sandbox Code Playgroud)
要么
not not int(input_value)
Run Code Online (Sandbox Code Playgroud)
哪个是Pythonic?有更多的Pythonic方式,然后上面提到的方式?
python ×10
boolean ×4
argparse ×1
checkbox ×1
fastq ×1
input ×1
json ×1
pandas ×1
parsing ×1
performance ×1
python-2.7 ×1
python-3.x ×1
replace ×1
set ×1