这是带有查询数据库的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'""")
表示游标中的结果: 打印结果
结果将是这样的,“L”的含义是什么
(367L,7200L,4543L,7200L,3216157.0)
但我想要这样
|事件|学习者| unique_learner |掉了| trans_price |
| 378 | 8092 | 5204 | 8092 | 3197704.0799999996 |
怎么办这样......请帮忙
答案 0 :(得分:1)
看起来您需要csv
格式的输出。您可以从cursor.description
获取列名。
试试这个:
with open('test.csv', 'w') as f:
writer = csv.writer(f, delimiter="|")
csv.writerow([col[0] for col in cursor.description])
for result in cursor:
writer.writerow(result)
test.csv
:
events|learners|unique_learner|dropped|trans_price
367|7200|4543|7200|3216157.0
或者,您可以使用字符串格式作为@Mr E建议,或仅通过" | ".join(...)
加入值,而不是使用csv模块。
希望有所帮助。