如何在python中更改接收的SQL输出格式

2024-05-01 21:58:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我只想从python中使用的sql查询得到简单的文本和输出

    import psycopg2
    conn = psycopg2.connect("dbname='news' user='postgres'")
    print("On which days did more than 1% of requests lead to errors? \n")
    cursor = conn.cursor()
    cursor.execute(
        "SELECT *FROM(SELECT date(time), round((count(status) FILTER (WHERE 
     NOT status ='200 OK'))::numeric * 100 / count(status), 2) AS 
     error_percentage FROM log GROUP BY date(time) ORDER BY error_percentage 
     DESC) AS I WHERE error_percentage > 1;" )

     results = cursor.fetchall()
     print (results)
     conn.close()

This is the output what i am getting.

我想要的是这样的输出。。。。。。。你知道吗

哪天超过1%的请求会导致错误? [2016年7月17日-2.27%误差]


Tags: fromdatebytimeasstatuscounterror
1条回答
网友
1楼 · 发布于 2024-05-01 21:58:24

你应该修复你的sql:用你想要的形式创建一个字符串。 像这样的

select to_char(date(time), 'FMDay, FMDD, FMYear') + " - " + to_char(dec_value) + "% errors"

否则您将得到数组作为返回。你知道吗

相关问题 更多 >