mysqldb cursor returning type along with result ?

Tim Chase python.list at tim.thechases.com
Sun Nov 29 08:54:35 EST 2009


> ((Decimal("101.10"),), (Decimal("99.32"),), (Decimal("97.95"),),
> (Decimal("98.45"),), (Decimal("97.39"),), (Decimal("97.91"),), (Decimal
> ("98.08"),), (Decimal("97.73"),))
> 
> as such :
>   sum(result)
> fails with "TypeError: unsupported operand type(s) for +: 'int' and
> 'tuple'"
> 
> How do I either get the resultset back as 'float' or convert the
> returned tuple to 'floats'.?

Well, what you have is a tuple-of-tuples-of-decimals, and Sum can 
handle Decimal types just fine.  You simply have to extract the 
first (only) item in each row:

   sum(row[0] for row in result)

or

   sum(value for (value,) in result)

whichever makes more sense to you.

-tkc






More information about the Python-list mailing list