My scenarios where I need it:
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 returnsTrueregardless since default bool function only returnsFalsewhen there is an empty string.Trueotherwise.I have a json for which I need it.
It has following representation:
list_of_visits ='''{"Museum": "True",
"Library": "True",
"Park": "False"}
'''
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
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)
trueandfalsein JSON.JSONDecodeError: Expecting value: line 1 column 12 (char 11)trueandfalsehave to be lowercase.'''{"Museum": true}'''"True"in your JSON? How is the receiver supposed to know which ones should be converted to booleans and which should be left alone? JSON syntax is unambiguous, that's one of it benefits.