如何在python中更改接收到的SQL输出格式
我只想从python中使用的sql查询中获得简单的文本和输出如何在python中更改接收到的SQL输出格式,python,sql,postgresql,Python,Sql,Postgresql,我只想从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
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()
我想要的是这样的输出
哪天超过1%的请求导致错误?
[2016年7月17日-2.27%的错误]您应该修复sql:按照您希望的格式创建一个字符串。 差不多
select to_char(date(time), 'FMDay, FMDD, FMYear') + " - " + to_char(dec_value) + "% errors"
否则您将获得数组作为返回。阅读有关
str.format()
的文档,但是如果我使用to\u char作为十进制值,那么我将无法使用where子句。在第二级select(上一级)的sql queryMake to_char的最后一行中提到了这一点