get raw decimal value from mysqldb query

jeffery_the_wind picture jeffery_the_wind · Sep 30, 2012 · Viewed 15.8k times · Source

I am executing a mysql query in python using the MySQLdb package. The code looks something like this:

c=db.cursor()
c.execute("""select * from table""")
output = []
for row in c:
    output.append(row[4])

where row[4] contains a decimal value that I want to store in the output list.

The problem is every value that I am getting looks like this: Decimal('XX.XX') where all I want in the output list is XX.XX. At the end of the script, my output list looks like this:

[Decimal('10.02'), Decimal('20.24'), ...]

But I need it to just contain the numbers, like this:

[10.02, 20.24, ...]

How do I do that?

Thanks!

Answer

Maksym Polshcha picture Maksym Polshcha · Sep 30, 2012
c=db.cursor()
c.execute("""select * from table""")
output = []
for row in c:
    output.append(float(row[4]))