Posts: 4,646
Threads: 1,493
Joined: Sep 2016
i am reading in data, splitting it, etc, and getting strings with 'True' and 'False', such as reading from python source code.
i want to convert these strings to boolean. if they were decimal numbers i could use int() or float(), but bool() ...
Output: Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> bool('True')
True
>>> bool('False')
True
>>>
... does not work that way.
so maybe i can do:
Output: Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 'False'=='True'
False
>>> 'True'=='True'
True
>>>
so far so good, but a bad string ...
Output: Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 'Maybe'=='True'
False
>>>
... does not do so well (an exception would be nice). any pythonic suggestions? converting 'true' and 'false' and 'TRUE' and 'FALSE' would be a plus.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 8,157
Threads: 160
Joined: Sep 2016
Jun-14-2017, 04:38 AM
(This post was last modified: Jun-14-2017, 04:38 AM by buran.)
def str_to_bool(my_str):
my_str = my_str.lower()
return my_str in ('true', 'false') and my_str == 'true'
for s in ('True', 'False', 'true', 'false', 'Maybe', 'maybe'):
print '{} -> {}'.format(s, str_to_bool(s)) Output: True -> True
False -> False
true -> True
false -> False
Maybe -> False
maybe -> False
or even
def str_to_bool(my_str):
return my_str.lower() == 'true'
for s in ('True', 'False', 'true', 'false', 'Maybe', 'maybe'):
print '{} -> {}'.format(s, str_to_bool(s)) Output: True -> True
False -> False
true -> True
false -> False
Maybe -> False
maybe -> False
Posts: 8,157
Threads: 160
Joined: Sep 2016
Jun-14-2017, 04:53 AM
(This post was last modified: Jun-14-2017, 04:56 AM by buran.)
Sorry, I didn't read it right - you want exception for not valid boolean string
def str_to_bool(my_str):
_my_str = my_str.lower()
if not _my_str in ('true', 'false'):
raise ValueError('{} has no boolean meaning'.format(my_str))
return _my_str == 'true'
for s in ('True', 'False', 'true', 'false', 'Maybe', 'maybe'):
try:
print '{} -> {}'.format(s, str_to_bool(s))
except ValueError as ve:
print ve[0] Output: True -> True
False -> False
true -> True
false -> False
Maybe has no boolean meaning
maybe has no boolean meaning
Posts: 2,953
Threads: 48
Joined: Sep 2016
Jun-14-2017, 05:16 AM
(This post was last modified: Jun-14-2017, 05:19 AM by wavic.)
I think this is the simplest way
def to_bool(text):
booleans = {'true': True, 'false': False}
return booleans[text.lower()] Or maybe @burans code
def str_to_bool(my_str):
return my_str.lower() == 'true'
Posts: 4,646
Threads: 1,493
Joined: Sep 2016
yeah, 'Maybe' has no special meaning, yet
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 8,157
Threads: 160
Joined: Sep 2016
def to_bool(text):
booleans = {'true': True, 'false': False}
return booleans[text.lower()] I like this one, only the exception in this case will be KeyError
Posts: 4,646
Threads: 1,493
Joined: Sep 2016
Jun-14-2017, 06:24 AM
(This post was last modified: Jun-14-2017, 06:30 AM by Skaperen.)
ok to shorten it a bit:
def to_bool(text):
return {'true': True, 'false': False}[text.lower()]
or:
def to_bool(text):
return {'true':True,'false':False,'yes':True,'no':False,'t':True,'f':False,'y':True,'n':False}[text.lower()]
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 2,953
Threads: 48
Joined: Sep 2016
(Jun-14-2017, 05:54 AM)buran Wrote: def to_bool(text):
booleans = {'true': True, 'false': False}
return booleans[text.lower()] I like this one, only the exception in this case will be KeyError
Hm!
It works here:
In [1]: def to_bool(text):
...: booleans = {'true': True, 'false': False}
...: return booleans[text.lower()]
...:
In [2]: to_bool('True')
Out[2]: True
In [3]: to_bool('False')
Out[3]: False
Posts: 8,157
Threads: 160
Joined: Sep 2016
Jun-14-2017, 09:31 AM
(This post was last modified: Jun-14-2017, 09:31 AM by buran.)
(Jun-14-2017, 09:06 AM)wavic Wrote: Hm!
It works here:
I mean, the error that will be raised if text is not 'true' or 'false', but e.g. 'maybe'
It's ok to raise it, becasue that is what Skaperen wants. Just mention that it will be different than the one I raise in my code above
Posts: 1
Threads: 0
Joined: Jun 2017
Oh nice topik, had found a lot of usefull info! Thanks!
|