简体   繁体   中英

Python select from database numeric format

    kurzor_databaza.execute("SELECT c0,c3,c6,c9,c12,c15,c18,c21 FROM teplota_tuv WHERE datum=(%s);",(datetime_object,))
    rows=kurzor_databaza.fetchall()
    hodnoty_databaza={}

    for row in rows:
      hodnoty_databaza['DEN']={'c0':row[0],'c3':row[1],'c6':row[2],'c9':row[3],'c12':row[4],'c15':row[5],'c18':row[6]}
      print hodnoty_databaza

I need delete Decimal('') from result.

When I run program This is result.

{'DEN': {'c9': Decimal('23.625'), 'c18': Decimal('22.625'), 'c3': Decimal('19.312'), 'c12': Decimal('23.062'), 'c0': Decimal('20.687'), 'c6': Decimal('19.75'), 'c15': Decimal('21.875')}}

Problems is Unsupported numeric type python.I need delete Decimal('') from result. Colums c0-c21 is numeric

Convert them to float:

hodnoty_databaza['DEN'] = {x:float(y) for x,y in hodnoty_databaza['DEN'].items()}
print hodnoty_databaza['DEN']
# {'c9': 23.625, 'c18': 22.625, 'c3': 19.312, 'c12': 23.062, 'c0': 20.687, 'c6': 19.75, 'c15': 21.875}

You can read more about Decimal .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM