CSC111 Cold England 2
--D. Thiebaut 11:24, 15 December 2011 (EST)
# ukTemperatures.py
# D. Thiebaut
# this code is undocumented and possibly not robust...
# Main site: http://www.metoffice.gov.uk/climate/uk/stationdata/
#
import urllib.request
import sys
URL = "http://cs.smith.edu/~111a/climate/uk/stationdata/"
ALTERNATE_URL = "http://cs.smith.edu/dftwiki/DFT/climate/uk/stationdata/"
cities = {'Ross-on-Wye' : 'rossonwyedata.txt',
'Armagh' : 'armaghdata.txt',
'Leuchars' : 'leucharsdata.txt',
'Eskdalemuir' : 'eskdalemuirdata.txt',
'Yeovilton' : 'yeoviltondata.txt',
'Valley' : 'valleydata.txt',
'Sutton Bonington': 'suttonboningtondata.txt',
'Dunstaffnage': 'dunstaffnagedata.txt',
'Shawbury' : 'shawburydata.txt',
'Paisley' : 'paisleydata.txt',
'Braemar' : 'braemardata.txt',
'Sheffield' : 'sheffielddata.txt' }
def getCityInfo( fileName ):
global URL
print( "Getting list of temperatures from ", URL )
f = urllib.request.urlopen( URL + fileName )
bytes = f.read()
htmlText = bytes.decode( "utf8" )
return htmlText
def getEarliest( text ):
for line in text.split( '\n' ):
if len( line )>1 and line[0]==' ':
try:
year = eval( line.split()[0] )
return year
except NameError:
continue
return None
def test():
# prompt the user for a city name
print( "What city are you interested in? " )
# display list of cities (keys of dictionary)
print( "Choices are: ", ", ".join( cities.keys() ) )
# keep on asking until city is recognized
while True:
city = input( "> " )
if city in cities:
break
print( "Sorry,", city,"is not a valid city name" )
# get text file associated with city
cityFile = cities[ city ]
# get Web page content
text = getCityInfo( cityFile )
print( '\n'.join( text.split('\n')[0:10] ) )
year = getEarliest( text )
print( "earliest = ", year )
def main():
for city in cities.keys():
year = getEarliest( getCityInfo( cities[ city ] ) )
print( city, year )
main()
[[Category:CS