Python parsing 'True' and 'False' as strings [duplicate]










0
















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










share|improve this question













marked as duplicate by vishes_shell, wim python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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 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















0
















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










share|improve this question













marked as duplicate by vishes_shell, wim python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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 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













0












0








0









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










share|improve this question















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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 18:17









NewToJSNewToJS

57431029




57431029




marked as duplicate by vishes_shell, wim python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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 python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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 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

















  • 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
















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












2 Answers
2






active

oldest

votes


















1














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'





share|improve this answer
































    0














    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.






    share|improve this answer





























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      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'





      share|improve this answer





























        1














        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'





        share|improve this answer



























          1












          1








          1







          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'





          share|improve this answer















          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'






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 19:04

























          answered Nov 13 '18 at 18:19









          AustinAustin

          9,8733828




          9,8733828























              0














              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.






              share|improve this answer



























                0














                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.






                share|improve this answer

























                  0












                  0








                  0







                  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.






                  share|improve this answer













                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '18 at 18:32









                  sam-pytsam-pyt

                  709820




                  709820













                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3

                      Museum of Modern and Contemporary Art of Trento and Rovereto