python - 如果语句返回错误的值

时间:2016-05-29 13:57:18

标签: python

我使用了跟随代码来触发基于行[0]的值的IF语句我的问题总是IF语句给我错误的输出,即使该值大于0也保持打印NO FAILURE。

conn = psycopg2.connect(conn_string)
        cursor = conn.cursor()
        cursor.execute("select count(*) as result from events_log where match_event_timestamp  > (NOW() - INTERVAL '147 hours');")
        row = cursor.fetchone()
        cursor.execute("SELECT COUNT(*) AS result FROM events_log  WHERE status = 'FAILURE' AND match_event_timestamp  > (NOW() - INTERVAL '24 hours');")
        row += cursor.fetchone()
        logger.info("Number of Events in last 24hr : %s ; Number of Event FAILURE  in last 24h: %s" % (row[0],row[1]))
        output = { 'api_key':'jhfsdf', 'data': { 'item' : [
                        {'text': 'Today ', 'value': row[0]},
                        {'text': 'Current Month ', 'value': row[1]}
                    ] } }

        if row[0] > '0':
                print('FAILURE')
        else:
                print("NO FAILURE")

我在IF声明中错过了什么?

1 个答案:

答案 0 :(得分:1)

'0'是字符串使用0。像这样:

if( row[0] > 0 ):
   ...

否则你得到这个:

TypeError: unorderable types: int() > str()

表示您无法将intstring进行比较。

相关问题