I am tasked with having to manually create a new directory (right-click, create new folder) in which to save my 48 weather files. The set-up I have is I am getting weather data from 4 cities from the web using: wunderground.com
Specifically, from this URL: https://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=KMDLAURE5&year=2018&month=2&graphspan=month&format=1
Am tasked with grabbing the data from that website, clean up the data, then save it.
In the problem I must access data from the internet 48 times, because their are 4 stations (cities) and 12 months. So I think I must create a file for each month and save all its monthly recorded temperature data. Then have all of those files stored into one directory file. I created a function for this:
import urllib
def accessData(ID, Month):
url="https://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=" + str(ID) + "&year=2017=7&month=" + str(Month) + "&graphspan=month&format=1"
infile = urllib.urlopen(url)
readline = infile.readlines()
infile.close()
return readline
Now I was given a separate file called stations.csv with contains the data:
KCASANFR131,37.778,-122.408
KDCWASHI48,38.913,-77.031
IBRITISH359,49.256,-123.245
KNYNEWYO639,40.755,-74.007
I know that, for example KCASANFR131 is the station ID but what 37.778,-122.408 is? Unsure of what that represents.
Also, should I create a list that stores the station IDs? and then call them in my nested loop, or is there a way to call those IDs from the csv file itself?
Now that I have the function it makes sense to create a nested loop outside of the function and then to call the function inside the nested loop. For for each station ID, that will be the outer loop, and the 12 months that will be the inner loop.
After it completes one full iteration it should return a list containing the results of that web request.
Here is the code for that (removed the header with an if-statement:
if I were to store that ID data in a list
stationID = [KCASANFR131, KDCWASHI48, IBRITISH359, KNYNEWYO639]
for i in range(1,13):
data = accessDat(i)
filename = "0{}.2017.csv".format(i)
outfile = open(filename, 'w')
row_count = len(data)
for j in range (2, row_count):
if(data[j] != '<br>\n' and data[j] != 'n'):
outfile.write(data[j])
outile.close()
Now what I am trying to do is keep that data separate from my scripts. So I want to save each file into a pre-made data directory using the format .csv Example: KDCWASHI48042017.csv
Also I need to have leading zeros, so the month should always be 2 characters long. For example, January will look like 01, and December will be 12, how does one do that using the str.zfill(2) method?
\n' and data[j] != 'n'):` statement has nothing affecting it because `outfile.write(data[j])` is on the same indentation – Antry Apr 11 '18 at 15:01