简体   繁体   中英

Python - How to create an array from a sql command with just the values?

I want to create an array from the data I got from my database.

My database looks something like this:

name, age, height, birth_month
John, 57,  2.11,   April 
Rico, 57,  1.05,   June 
Max,  57,  1.50,   December 
Lisa, 35,  1.23,   July 
Beth, 21,  1.66,   July 
Luna, 89,  2.3,    July`

Now I want an array excluding name and putting birth_month first. Here is the array I want to create:

data = [[April,     57,  2.11],
        [June,      57,  1.05],
        [December,  57,  1.50],
        [July,      35,  1.23],
        [July,      21,  1.66],
        [July,      89,  2.3 ]]

So what I tried to do was this:

mycursor = mydb.cursor()
mycursor.execute("SELECT birth_month, age, height FROM customers")
print(mycursor.fetchall())

But this is not exactly what I want. How can I get an array formatted, just as I want?


This is what I get:

[[(u'April', Decimal('57'), Decimal('2.11')),
(u'June', Decimal('57'), Decimal('1.05')),
(u'December', Decimal('57'), Decimal('1.50')),
(u'July', Decimal('35'), Decimal('1.23')),
(u'July', Decimal('21'), Decimal('1.66')),
(u'July', Decimal('89'), Decimal('2.3'))]]

What I want is that the "Decimal" and "u'" dissapears, and that every row has seperate [].

mycursor.execute("SELECT name,age,height FROM customers")
print(mycursor.fetchall())

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