[Tutor] Writing for loop output to csv

Alan Gauld alan.gauld at yahoo.co.uk
Thu Sep 6 18:04:36 EDT 2018


On 06/09/18 17:32, Brandon Creech wrote:
> 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

Sorry, can you elaborate I don;t understand what you mean?
The data you show does not seem to be related to the output
you show?

Also having the city be a dual name separated by a comma
within a csv file is likely to create confusion, not least
for yourself. I would suggest splitting it to city and
country. If that doesn't work (for your API perhaps)
then consider changing the separator.  If that doesn't
work for you then tough luck, you'll need to persevere.


> from forecastiopy import *
> import csv

You import csv but don't seem to use it anywhere below?

> 
> api_key = 'XXXXXXX'
> 
> cities = {"Taree, Australia":(-31.8895,152.4444), "Belmopan,
> Belize":(17.251,-88.759), "Nanaimo, Canada":(49.1659,-123.9401),

Again this does not seem to match either the the
data sample shown above or the output.

> 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']))

This will produce something like the output you showed
(although I don't think you need all the str() calls).
But it is nothing like the output you said you wanted
in the csv file and you are not using csv anywhere
to generate it.

What exactly are you asking us to help you with?
Everything seems to be doing exactly what you are
asking it to do.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list