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