Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8597435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:54:45+00:00 2026-06-12T00:54:45+00:00

I am executing a mysql query in python using the MySQLdb package. The code

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:54:46+00:00Added an answer on June 12, 2026 at 12:54 am

    You can either convert a Decimal object to a string:

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

    Or to a float:

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

    Converting it to a float will cause it to lose its full precision, so a value like 20.24 will become 20.239999999999998.

    Also, casting it to a float will raise an Exception if the value is None. To avoid that, you can use a helper function like this:

    def convert_mysql_decimal_to_float(decimal_object):
        if (decimal_object == None):
            return None
        else:
            return float(decimal_object)
    
    cell_value = convert_mysql_decimal_to_float(row[4])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a select query in mysql . On each execution of this query
I have the following piece of code, executing a pretty simple MySQL query: $netnestquery
I have this elementary query: SELECT d.description, o.code FROM order_positions AS o LEFT JOIN
Following code, using python 2.6.6 and MySQLdb 1.2.2 causes Commands out of sync; you
What I'd like to do is execute a MySQL query containing a where clause
While executing the below mysql query in rails cnt = Domainurl.find_by_sql [SELECT Max(`count`)FROM `domainurls`
I am passing '2012-09-10 00:00:00-05:00' to a MySQL query. This value is retrieved using
What I want to do is while executing MySQL query, passing column data into
I have this query which on executing in my sql command line client executes
The following code is executing perfectly but there is a mySQL error somewhere that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.