Python parsing 'True' and 'False' as strings [duplicate]
This question already has an answer here:
Converting from a string to boolean in Python?
26 answers
Is there a way in python to parse the string 'True'
as True
(boolean) and 'False'
as False
(boolean)?
I know I could do bool('True')
or bool('False')
but each would be True
python parsing boolean
marked as duplicate by vishes_shell, wim
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 13 '18 at 18:59
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Converting from a string to boolean in Python?
26 answers
Is there a way in python to parse the string 'True'
as True
(boolean) and 'False'
as False
(boolean)?
I know I could do bool('True')
or bool('False')
but each would be True
python parsing boolean
marked as duplicate by vishes_shell, wim
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 13 '18 at 18:59
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Take a look atast.literal_eval()
.
– Vasilis G.
Nov 13 '18 at 18:19
Can’t test because I’m on my phone, can you use eval()?
– Adam Mitchell
Nov 13 '18 at 18:19
Look at @Austin's answer.
– Vasilis G.
Nov 13 '18 at 18:20
Why not simply usestring == 'True'
?
– usr2564301
Nov 13 '18 at 18:23
add a comment |
This question already has an answer here:
Converting from a string to boolean in Python?
26 answers
Is there a way in python to parse the string 'True'
as True
(boolean) and 'False'
as False
(boolean)?
I know I could do bool('True')
or bool('False')
but each would be True
python parsing boolean
This question already has an answer here:
Converting from a string to boolean in Python?
26 answers
Is there a way in python to parse the string 'True'
as True
(boolean) and 'False'
as False
(boolean)?
I know I could do bool('True')
or bool('False')
but each would be True
This question already has an answer here:
Converting from a string to boolean in Python?
26 answers
python parsing boolean
python parsing boolean
asked Nov 13 '18 at 18:17
NewToJSNewToJS
57431029
57431029
marked as duplicate by vishes_shell, wim
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 13 '18 at 18:59
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by vishes_shell, wim
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 13 '18 at 18:59
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Take a look atast.literal_eval()
.
– Vasilis G.
Nov 13 '18 at 18:19
Can’t test because I’m on my phone, can you use eval()?
– Adam Mitchell
Nov 13 '18 at 18:19
Look at @Austin's answer.
– Vasilis G.
Nov 13 '18 at 18:20
Why not simply usestring == 'True'
?
– usr2564301
Nov 13 '18 at 18:23
add a comment |
Take a look atast.literal_eval()
.
– Vasilis G.
Nov 13 '18 at 18:19
Can’t test because I’m on my phone, can you use eval()?
– Adam Mitchell
Nov 13 '18 at 18:19
Look at @Austin's answer.
– Vasilis G.
Nov 13 '18 at 18:20
Why not simply usestring == 'True'
?
– usr2564301
Nov 13 '18 at 18:23
Take a look at
ast.literal_eval()
.– Vasilis G.
Nov 13 '18 at 18:19
Take a look at
ast.literal_eval()
.– Vasilis G.
Nov 13 '18 at 18:19
Can’t test because I’m on my phone, can you use eval()?
– Adam Mitchell
Nov 13 '18 at 18:19
Can’t test because I’m on my phone, can you use eval()?
– Adam Mitchell
Nov 13 '18 at 18:19
Look at @Austin's answer.
– Vasilis G.
Nov 13 '18 at 18:20
Look at @Austin's answer.
– Vasilis G.
Nov 13 '18 at 18:20
Why not simply use
string == 'True'
?– usr2564301
Nov 13 '18 at 18:23
Why not simply use
string == 'True'
?– usr2564301
Nov 13 '18 at 18:23
add a comment |
2 Answers
2
active
oldest
votes
Use ast.literal_eval
:
>>> import ast
>>> ast.literal_eval('False')
False
If you do type(ast.literal_eval('False'))
, you see <class 'bool'>
:
>>> type(ast.literal_eval('False'))
<class 'bool'>
You could also write your own function that returns 'True'
as boolean True
, 'False'
as boolean False
and if you supply any other input, it returns the same back:
def parse(string):
d = 'True': True, 'False': False
return d.get(string, string)
Now, you call as:
>>> parse('True')
True
>>> parse('False')
False
>>> parse('Anything')
'Anything'
add a comment |
In this case I would not recommend ast.literal_eval
or eval
. The best thing to do is probably this:
def parse_boolean(b):
return b == "True"
"True"
will return True
and "False"
will return False
.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use ast.literal_eval
:
>>> import ast
>>> ast.literal_eval('False')
False
If you do type(ast.literal_eval('False'))
, you see <class 'bool'>
:
>>> type(ast.literal_eval('False'))
<class 'bool'>
You could also write your own function that returns 'True'
as boolean True
, 'False'
as boolean False
and if you supply any other input, it returns the same back:
def parse(string):
d = 'True': True, 'False': False
return d.get(string, string)
Now, you call as:
>>> parse('True')
True
>>> parse('False')
False
>>> parse('Anything')
'Anything'
add a comment |
Use ast.literal_eval
:
>>> import ast
>>> ast.literal_eval('False')
False
If you do type(ast.literal_eval('False'))
, you see <class 'bool'>
:
>>> type(ast.literal_eval('False'))
<class 'bool'>
You could also write your own function that returns 'True'
as boolean True
, 'False'
as boolean False
and if you supply any other input, it returns the same back:
def parse(string):
d = 'True': True, 'False': False
return d.get(string, string)
Now, you call as:
>>> parse('True')
True
>>> parse('False')
False
>>> parse('Anything')
'Anything'
add a comment |
Use ast.literal_eval
:
>>> import ast
>>> ast.literal_eval('False')
False
If you do type(ast.literal_eval('False'))
, you see <class 'bool'>
:
>>> type(ast.literal_eval('False'))
<class 'bool'>
You could also write your own function that returns 'True'
as boolean True
, 'False'
as boolean False
and if you supply any other input, it returns the same back:
def parse(string):
d = 'True': True, 'False': False
return d.get(string, string)
Now, you call as:
>>> parse('True')
True
>>> parse('False')
False
>>> parse('Anything')
'Anything'
Use ast.literal_eval
:
>>> import ast
>>> ast.literal_eval('False')
False
If you do type(ast.literal_eval('False'))
, you see <class 'bool'>
:
>>> type(ast.literal_eval('False'))
<class 'bool'>
You could also write your own function that returns 'True'
as boolean True
, 'False'
as boolean False
and if you supply any other input, it returns the same back:
def parse(string):
d = 'True': True, 'False': False
return d.get(string, string)
Now, you call as:
>>> parse('True')
True
>>> parse('False')
False
>>> parse('Anything')
'Anything'
edited Nov 13 '18 at 19:04
answered Nov 13 '18 at 18:19
AustinAustin
9,8733828
9,8733828
add a comment |
add a comment |
In this case I would not recommend ast.literal_eval
or eval
. The best thing to do is probably this:
def parse_boolean(b):
return b == "True"
"True"
will return True
and "False"
will return False
.
add a comment |
In this case I would not recommend ast.literal_eval
or eval
. The best thing to do is probably this:
def parse_boolean(b):
return b == "True"
"True"
will return True
and "False"
will return False
.
add a comment |
In this case I would not recommend ast.literal_eval
or eval
. The best thing to do is probably this:
def parse_boolean(b):
return b == "True"
"True"
will return True
and "False"
will return False
.
In this case I would not recommend ast.literal_eval
or eval
. The best thing to do is probably this:
def parse_boolean(b):
return b == "True"
"True"
will return True
and "False"
will return False
.
answered Nov 13 '18 at 18:32
sam-pytsam-pyt
709820
709820
add a comment |
add a comment |
Take a look at
ast.literal_eval()
.– Vasilis G.
Nov 13 '18 at 18:19
Can’t test because I’m on my phone, can you use eval()?
– Adam Mitchell
Nov 13 '18 at 18:19
Look at @Austin's answer.
– Vasilis G.
Nov 13 '18 at 18:20
Why not simply use
string == 'True'
?– usr2564301
Nov 13 '18 at 18:23