Convert True/False value read from file to boolean

前端 未结 14 874

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:47

    you can use distutils.util.strtobool

    >>> from distutils.util import strtobool
    
    >>> strtobool('True')
    1
    >>> strtobool('False')
    0
    

    True values are y, yes, t, true, on and 1; False values are n, no, f, false, off and 0. Raises ValueError if val is anything else.

提交回复
热议问题