Convert True/False value read from file to boolean

前端 未结 14 879

I\'m reading a True - False value from a file and I need to convert it to boolean. Currently it always converts it to True even if the value is set

14条回答
  •  南方客
    南方客 (楼主)
    2020-12-07 18:50

    If you want to be case-insensitive, you can just do:

    b = True if bool_str.lower() == 'true' else False
    

    Example usage:

    >>> bool_str = 'False'
    >>> b = True if bool_str.lower() == 'true' else False
    >>> b
    False
    >>> bool_str = 'true'
    >>> b = True if bool_str.lower() == 'true' else False
    >>> b
    True
    

提交回复
热议问题