0

次のコードを使用して、Python で MySql クエリを実行しています。

cur = conn.cursor()
query = ("""select sum(if(grade is not null,1,0)) as `test`,
    sum(if(grade < 7,1,0)) as `bad`,
    sum(if(grade < 7,1,0))/sum(if(grade is not null,1,0))*100 as `Pct Bad Grade`,
    student
    from grades_database
    where test_date >= '2014-08-01'
    group by student
    order by `Pct Bad Grade` desc;""")
cur.execute(query)

そして、次のような結果が得られます

(Decimal('1'), Decimal('3'), Decimal('50.0000'), 'John Doe')

値の前にある "Decimal" 文字列を削除する必要があります。

過去の例から以下を使用してみました

output = []
for row in cur:
    output.append(float(row[0]))
print output

しかし、それは私にこれを与えます

[20.0, 5.0, 3.0, 7.0, 7.0, 2.0, 6.0, 7.0, 7.0, 7.0,...]

理想的には、出力の順序を並べ替えて、次のようなものを取得したいと思います

(John Doe, 50, 3, 1)]

から

(Decimal('1'), Decimal('3'), Decimal('50.0000'), 'John Doe')
4

1 に答える 1