Converting “true” (JSON) to Python equivalent “True”

后端 未结 7 1905
感动是毒
感动是毒 2020-12-23 21:21

The Train status API I use recently added two additional key value pairs (has_arrived, has_departed) in the JSON object, which caused my script to crash.

<
7条回答
  •  忘掉有多难
    2020-12-23 21:48

    It is possible to utilize Python's boolean value for int, str, list etc.

    For example:

    bool(1)     # True
    bool(0)     # False
    
    bool("a")   # True
    bool("")    # False
    
    bool([1])   # True
    bool([])    # False
    

    In Json file, you can set

    "has_arrived": 0,
    

    Then in your Python code

    if data["has_arrived"]:
        arrived()
    else:
        not_arrived()
    

    The issue here is not to confuse 0 indicated for False and 0 for its value.

提交回复
热议问题