[Tutor] Writing for loop output to csv

Brandon Creech bdcreech91 at gmail.com
Thu Sep 6 12:32:37 EDT 2018


Hi, I am working to forecast the temperatures for the next 5 days using an
API and a for loop. I would like write the output of this loop to a csv in
this format::

Columns: City, min1, max1, min2, max2,min3,max3,min4,max4,min5,max5
data:         Athens,Greece 25.4,26.7....etc.
                 Nantou,Taiwan 16.18, ......etc

the data prints out on top of each other like this:

Nantou, Taiwan 29.49 20.79
Nantou, Taiwan 30. 49 21.56

Code I have:

from forecastiopy import *
import csv

api_key = 'XXXXXXX'

cities = {"Taree, Australia":(-31.8895,152.4444), "Belmopan,
Belize":(17.251,-88.759), "Nanaimo, Canada":(49.1659,-123.9401),
       "Sherbrooke,Canada":(45.4042,-71.8929),
"Shenyang,China":(41.8057,123.4315),"Crewe,England (UK)":(53.1004,-2.4438),
       "Cairo, Egypt":(30.0444,31.2357),
"Kaiserslautern,Germany":(49.4401,7.7491),
"Athens,Greece":(37.9838,23.7275),
       "Bengaluru,India":(12.9716,77.5946),
"Incheon,Korea":(37.4563,126.7052), "Kathmandu, Nepal":(27.7172,85.3240),

 "Lahore,Pakistan":(31.5204,74.3587),"Lima,Peru":(-12.0464,-77.0428),"Belgrade,Serbia":(44.7866,20.4489),
       "Nantou,Taiwan":(23.961,120.9719),"Des Moines,United
States":(41.5868,-93.625)}



for city, coords in cities.items():
    weather = ForecastIO.ForecastIO( api_key, latitude=coords[0],
longitude=coords[1] )
    daily = FIODaily.FIODaily(weather)
    for day in range(2,7):
        print(str(city) + " " + str(daily.get_day(day)['temperatureMax']) +
" " + str(daily.get_day(day)['temperatureMin']))


More information about the Tutor mailing list