python - 如何将输出格式化为表格

标签 python mysql

这是查询数据库的Python代码

cursor.execute("""select count(distinct offering_name) as events
    ,count(learner) as learners 
    ,count(distinct learner) as unique_learner
    ,count(dropped) as dropped
    ,sum(trans_price) as trans_price
    from EMS_data where organization like 'eng%' and manager ='rakesh'""")

#result= cursor.fetchall()
for result in cursor:
    print result

结果会是这样的

(367L, 7200L, 4543L, 7200L, 3216157.0)

但我想喜欢这个

+--------+----------+----------------+---------+--------------------+
| events | learners | unique_learner | dropped | trans_price        |
+--------+----------+----------------+---------+--------------------+
|    378 |     8092 |           5204 |    8092 | 3197704.0799999996 |
+--------+----------+----------------+---------+--------------------+

这样怎么办...请帮忙

最佳答案

我不明白你的问题,有什么问题吗?输出格式?它是一个元组,您可以使用括号 [ ] 访问它们,并在内部放置索引,从 0 开始。

打印结果[0] #prints 367

然后你可以自己格式化它。

关于python - 如何将输出格式化为表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17025659/

相关文章:

python - 类属性查找规则?

mysql - 如何从基于 where 子句和子查询的数据列表中派生标题

mysql - 删除时使用默认值更新外键引用

python - 捕获输入按下事件

python - 在 python 3.x 中创建、修改和保存图像

java - 将空值 JTextFormatted 插入双列到 mysql

java - JPQL 使用日期时间的日期部分查询实体的 java.util.Date 字段

php - 如何通过 CGI 接口(interface)调用我的 C++ 二进制文件?

python - Python 中的扫雷器(使用 tkinter)

python - Sympy:如何将乘积的对数简化为对数和?