I'm wondering how, in Python, I could convert strings like:
- "(True & False) | True"
- "((False | False) & (True | False)) & (False | True)"
To a boolean answer:
- True
- False
The bool() function doesn't seem to work.
Thanks,
You can simply eval these expressions as they are valid Python
>>> eval("(True & False) | True")
True
>>> eval("((False | False) & (True | False)) & (False | True)")
False
eval might have unwanted characters in it, in your case, you can use the following code: import re;s = "((False | False) & (True | False)) & (False | True)";if not re.sub(r'[\(\)\|&TrueFals ]*', '', s):eval(s). This strips out everything other than True, False, etc. If anything remains then the eval would not be executed.