-1

I'm fetching some temperature data from MySQL which i receive in a tuple like this: ((Decimal('28.7'),), (Decimal('28.7'),), (Decimal('28.6'),))

I can't seem to figure out how to get the average of these values out.

Hoping anyone knows and it's an easy fix? :)

Sometimes these tuples contain more data, sometimes less, but as said the goal is to get the average out somehow..

This is my code as of now:

db = MySQLdb.connect("localhost","user","pass","weather")
cursor = db.cursor()
sql = "SELECT "+dbcolumn+" FROM weather_data WHERE DATETIME > NOW() - INTERVAL 15 MINUTE"
cursor.execute(sql)
fetched = cursor.fetchall()
cursor.close()
db.close()

#This prints out each item in the tuple nicely on separate rows
for i in fetched:
    print(i[0])

print("next is the average:")

#This currently prints the tuple, but should be modified to print the average of the tuple instead
print(fetched)

Thanks guys!

EDIT: I ended up making a function that returns me the average from the tuple instead.. This is that function:

#This function finds the average in a tuple, as from the minutedata function
def averager(incoming):
    datasummary = 0
    itemsnumber = 0
    for i in incoming:
        datasummary = datasummary + i[0]
        itemsnumber = itemsnumber + 1
    dataaverage = datasummary / itemsnumber
    return dataaverage
Bill Skullman
  • 33
  • 1
  • 7
  • 1
    It's good form to post a reasonable and minimal example of your current code. It shows you're not just asking people to do all the work for you, and can allow people like me who are not very familiar with your programming language of choice to offer solutions based on general logic, or offer query tweaks to have MySQL do the work for you. – Uueerdo Jun 15 '18 at 20:39
  • Hi @Uueerdo. Thanks for your response, and sorry if I was being short in the question. I was simply not sure what good it would do to post code, as I can't seem to figure out how to achieve that result. db = MySQLdb.connect("localhost","user","pass","weather") cursor = db.cursor() sql = "SELECT TEMPERATURE FROM weather_data WHERE DATETIME > NOW() - INTERVAL 15 MINUTE" cursor.execute(sql) fetched = cursor.fetchall() cursor.close() db.close() So this returns the fetched tuple that looks like in my question. – Bill Skullman Jun 15 '18 at 20:47
  • You'll need to add that to the question, and use "Code Sample" formatting to preserve the formatting; one of the few things I remember from playing with python years ago is that indentation matters. – Uueerdo Jun 15 '18 at 20:51
  • But [this post](https://stackoverflow.com/questions/34463901/how-to-iterate-through-cur-fetchall-in-python) looks like it could point you in the right direction, even if no answer was accepted. – Uueerdo Jun 15 '18 at 20:54
  • I added my code @Uueerdo. And I checked your post but I still feel stuck unfortunately :( – Bill Skullman Jun 15 '18 at 20:58
  • @BillSkullman You are allowed to answer your own question. I think it is even better to make an answer than an edit in this case (cf. https://meta.stackoverflow.com/questions/339050/when-to-post-new-answer-vs-overhaul-edits) – 5th Jun 15 '18 at 21:52

2 Answers2

0

If you want to get the average without fetching the data you can use the AVG MYSQL function directly in your query

Yosra Hamza
  • 529
  • 3
  • 5
0

I ended up making a function that returns me the average from the tuple instead.. This is that function:

#This function finds the average in a tuple, as from the minutedata function
def averager(incoming):
    datasummary = 0
    itemsnumber = 0
    for i in incoming:
        datasummary = datasummary + i[0]
        itemsnumber = itemsnumber + 1
    dataaverage = datasummary / itemsnumber
    return dataaverage
Bill Skullman
  • 33
  • 1
  • 7