如何在python中将bool的字符串表示形式解析为bool? [英] How to parse string representations of bool into bool in python?

查看:101
本文介绍了如何在python中将bool的字符串表示形式解析为bool?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的场景:

  1. 用户输入的是True或False,默认情况下将其解析为str.我无法将解析类型更改为python 3+解析为str(使用Python 3.8)因此,当我解析 bool(input('Enter True or False'))时,无论默认布尔函数何时仅返回 False ,它都会返回 True .有一个空字符串.否则为 True .

  1. User is inputting True or False and it is parsed as str by default. I cannot change the parsing type as python 3+ parses as str (Using Python 3.8) So when I parse bool(input('Enter True or False')), it returns True regardless since default bool function only returns False when there is an empty string. True otherwise.

我有一个需要它的json.

I have a json for which I need it.

它具有以下表示形式:

list_of_visits ='''{"Museum": "True",
"Library": "True",
"Park": "False"}
'''

请注意,如果没有qoutes,我将无法表示为:

Note that I cannot have its representation without qoutes as:

list_of_visits ='''{"Museum": True,
"Library": True,
"Park": False}
'''

原因如下,将引发错误:

Cause parsing this as follows throws error:

jsonic = json.loads(list_of_visits)
jsonic

但是在某些地方,我还需要从int和float进行解析,并且我无法分别为每种类型编写函数.我正在尝试构建一站式解决方案,该解决方案可能会继承bool()的功能并进行此转换,因此我可以在任何地方使用它即不仅可以执行传统的bool()操作,而且还可以解析字符串表示形式的解决方案.

But I need to parse from int and float as well at some places and I cannot write functions for each type separately . I am trying to build a one-stop solution which might inherit functionality from bool() and does this conversion as well and I can use it everywhere i.e. solution which can not only perform traditional bool() operations but also able to parse string representations.

目前,我正在执行以下步骤:

Currently I do following steps:

if type(value) == str and value =='True':
    reprs = True
elif type(value) == str and value =='False':
    reprs = False
else:
    reprs = bool(value)

推荐答案

您可以定义自己的函数,该函数在以下所有情况下都可以工作:

You can define your own function which may work in all scenarios as:

def get_bool(key):
    return value if value := {'True':True, 'False':False}.get(key) else bool(key)
    

对于单个值(例如输入),您可以将其解析为:

get_bool('False')

返回: False

from toolz.dicttoolz import valmap
valmap(get_bool, jsonic)

返回:

{'Museum': 1, 'Library': 1, 'Park': 0}
    

对于3.7及更低版本:

自从它在python 3.8中问世以来,我就爱上了walrus运算符.任何寻找较低版本的人都可能需要重复:

For 3.7 and lower:

I'm in love with walrus operator since it came out in python 3.8. Anyone looking for lower versions might need some repetitions:

def get_bool(key):
return {'True':True, 'False':False}.get(key) if  key.title() in {'True':True, 'False':False}.keys() else bool(key)

请注意,尽管它确实适用于每种情况,但您的JSON表示形式是错误的.JSON可以具有布尔值,JSON的字符串表示形式也可以.但是您必须将JavaScript语法用作 true false ,而不是Python的 True & False .由于JSON是一种JavaScript符号.

Note that though it does work for every case, your JSON representation is wrong. JSONs can have boolean values so can the string representations of JSONs. But you got to use javascript syntax as true and false instead of Python's True & False. Since JSON is a javascript notation.

这篇关于如何在python中将bool的字符串表示形式解析为bool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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