Export (or write) output values from a function to a csv file

Viewed 16

I have function that uses an external csv to interrogate weather data, and return an output that provides the following information:

Days over 40 degrees celsius in Albury : 42 Days over 40 degrees celsius in Perth : 28 Days over 40 degrees celsius in Melbourne : 16 Days over 40 degrees celsius in Canberra : 6 Days over 40 degrees celsius in Sydney : 7 Days over 40 degrees celsius in Brisbane : 0 Days over 40 degrees celsius in Darwin : 0 City with most number of days above 40 degrees celsius is Albury

This is a only a small section of the first function code

def daysHot(dictionaryData,location,temp): templist=dictionaryData[location] count=0 for i in templist: if(float(i)>temp): count+=1 return count

if name == 'main':

mydict = {} #creating dictionary with city as key and temp(list) as value
with open('/Users/xxx/Desktop/weather.csv', mode='r') as inp:
    reader = csv.reader(inp)
    i=0
    for rows in reader:
        if(i==0):
            i+=1
            continue #this will not include the column header
        else:
            if rows[1] in mydict.keys():
                l=mydict[rows[1]]
                l.append(rows[3])
                mydict[rows[1]]=l
            else:
                mydict[rows[1]]=[rows[3]]

#Note that this is not the first function in its entirety.

A second function is needed to then use the output...

Albury, 42 Perth, 28 Melbourne, 16 Canberra, 6 Sydney, 7 Brisbane, 0 Darwin, 0

and export it to a new csv file. If the temperature variable is changed in the first function then the csv file updates accordingly in the new csv file. There is already a similar question in SO, however it does not resolve my issue. Can't seem to get the "write" function to do what it should do.

Albury, 42 Perth, 28 Melbourne, 16 Canberra, 6 Sydney, 7 Brisbane, 0 Darwin, 0

I considered starting with the following (which is from a similar example on SO).

def writeTempsHot(Temperature):

mydict = {
       '1':   {'Location': 'Albury','Temp Count':''},
       '2':   {'Location': 'Perth','Temp Count':''},  etc...

Just a little lost on this code. Hope this makes sense.

0 Answers
Related