0

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 [].

2
  • Could you explain in more detail how the output you get is different from what you want? Commented Oct 23, 2018 at 19:05
  • I added my result to the question below Commented Oct 24, 2018 at 9:51

1 Answer 1

2
mycursor.execute("SELECT name,age,height FROM customers")
print(mycursor.fetchall())
Sign up to request clarification or add additional context in comments.

10 Comments

I also want to remove the u' and Decimal()
those arent really there ... its just a representation print(u"hello") or print(Decimal("1.0"))
So the array that I get is correct, with what I want?
probably ... its the values that you want ... but if you are submitting it to an autograder or something you might need to finesse the data a tiny bit more
I want to use it in formulas like y = x + Variable. Does it work?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.