Home>
if (int (dict_json ['report_num']) == 1) and (dict_json ['report_time']! = BEFORE_dict_json ['report_time']):


In this code, nothing may be assigned todict_json ['report_num']. At this time,

ValueError
invalid literal for int () with base 10: ''


It will become. What if I want to implement functions such astryandexceptin the conditional expression part of an if statement? By the way, if nothing is assigned, you don't want to execute the code in the if statement.

try:
    bool = (int (dict_json ['report_num']) == 1) and (dict_json ['report_time']! = BEFORE_dict_json ['report_time'])
except ValueError:
    bool = False
if bool:


Is there no choice but to do that?

  • Answer # 1

    If you don't digitize it.

    if (dict_json ['report_num'] == '1') and (dict_json ['report_time']! = BEFORE_dict_json ['report_time']):
        ...

    It's true that the condition is too long to read, so you can prepare a function appropriately.

    def cond (n_json, before_json): # Please rename the function appropriately according to the purpose
        if n_json ['report_num']! = '1':
            return False
        return n_json ['report_time']! = before_json ['report_time']
    if cond (dict_json, BEFORE_dict_json):
        ...