Skip to content

Instantly share code, notes, and snippets.

@mamaj
Last active August 27, 2022 04:41
Show Gist options
  • Save mamaj/02996e288cb9bcd06f2c60f26a38f7a9 to your computer and use it in GitHub Desktop.
Save mamaj/02996e288cb9bcd06f2c60f26a38f7a9 to your computer and use it in GitHub Desktop.
python parse string value as boolean
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