[Level 1] How to evaluate the string to boolean?
If you want to evaluate the string you could create a function like below.
But you could have an easier way to implement with native module called "ast.literal_eval()"
e.g.
#!/bin/env python def toBoolean(str): return if str in ['True', '1'] print toBoolean('True') print toBoolean('False')
But you could have an easier way to implement with native module called "ast.literal_eval()"
e.g.
>>> import ast >>> help(ast.literal_eval) Help on function literal_eval in module ast: literal_eval(node_or_string) Safely evaluate an expression node or a string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None. >>> ast.literal_eval('False') False >>> ast.literal_eval('True') True >>> ast.literal_eval('1') 1 >>> ast.literal_eval('0') 0Wish this helps. regards, Stanley Huang
Comments
Post a Comment