Python-将数组转换为表格视图

时间:2018-09-05 13:39:22

标签: python python-3.x listview tabular

我有一个Python函数,可以从表中提取数据。以下是我的代码和示例输出

import psycopg2
#import pandas as pd
from tabulate import tabulate

## Login to Data Warehouse
def data_warehouse_login():
    global dwh_connection
    global dwh_cur
    try:
        dwh_connection = psycopg2.connect(db_details)
        dwh_cur = dwh_connection.cursor()
    except:
        print("I am unable to connect to the database.")
    def data_warehouse_close():
        dwh_cur.close()
        dwh_connection.close()

def count():
    data_warehouse_login()
    cur = dwh_connection.cursor()
    cur.execute("""select count(*),to_char(created_at,'yyyy-mm') from  sales   group by to_char(created_at,'yyyy-mm') order by 2 asc""")
    result = cur.fetchall()
    fol_count = result
    print(str(fol_count))
    dwh_connection.close()

输出如下:

[(100, '2018-04'), (433, '2018-05'), (232, '2018-06'), (400, '2018-07'), (900, '2018-08')]

尝试使用列表库,但我无法获得所需的格式

from tabulate import tabulate 
print (tabulate([["Sale", "Date"], [fol_count[0],fol_count[1]]],headers = "firstrow"))

谁能帮我解决这个问题。谢谢

0 个答案:

没有答案