Last active
August 27, 2022 04:41
-
-
Save mamaj/02996e288cb9bcd06f2c60f26a38f7a9 to your computer and use it in GitHub Desktop.
python parse string value as boolean
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def parse_bool_str(val): | |
if isinstance(val, str): | |
val = val.lower().strip() | |
if val in ['true', 'yes', 'y', '1']: | |
val = True | |
elif val in ['false', 'no', 'n', '0']: | |
val = False | |
else: | |
raise ValueError('Provided value cannot be parsed as a string.') | |
else: | |
val = bool(val) | |
return val |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment