Crossin的编程教室

标题: mysql取数问题 [打印本页]

作者: 很白很白的小白    时间: 2020-9-7 18:16
标题: mysql取数问题
各位大大好:
新人小白求助。总的目的就是想通过python连接数据库进行一些处理,然后输出到excel

在连接了数据库之后,取数遇到了问题。
连接了数据库,取数。但是用fetchall,取出来的数包含字段名-story和estimate
[ {'story': 34289, 'estimate': 0.0}, {'story': 26791, 'estimate': 0.0}, {'story': 26791, 'estimate': 8.0}]
取出的是类似于以上的含dictionary的一个list
我想把story一样的项目做一个分类汇总.
就是类似得到以下这样的表:
story    estimate
34289   0
26791   8
请问该如何操作?先谢过了。
目前的代码是:

try:

    with connection.cursor() as cursor:
        sql = "select story, estimate FROM zt_task"
        cursor.execute(sql)
        result = cursor.fetchall()
        for row in result:
             print(result)
后面就不知道怎么操作了,想了各种方法,包括counter等等。还是技术有限。。。
先谢过了~
作者: 很白很白的小白    时间: 2020-9-8 08:36
请问有大神么~
作者: crossin先生    时间: 2020-9-8 22:37
  1. import pandas
  2. x=[ {'story': 34289, 'estimate': 0.0}, {'story': 26791, 'estimate': 0.0}, {'story': 26791, 'estimate': 8.0}]
  3. t = pandas.DataFrame(x)
  4. t.to_excel('xxx.xlsx')
复制代码





欢迎光临 Crossin的编程教室 (https://bbs.crossincode.com/) Powered by Discuz! X2.5