CSC111 How cold is it in England?
--D. Thiebaut 22:57, 12 December 2011 (EST)
# ukTemperatures.py
# D. Thiebaut
# this code is undocumented and possibly not robust...
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 getCity( cityName ):
global URL
print( "Getting list of temperatures from ", url )
f = urllib.request.urlopen( URL + cityName )
bytes = f.read()
htmlText = bytes.decode( "utf8" )
return htmlText
def main():
# 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.keys():
break
print( "Sorry,", city,"is not a valid city name" )
# get text file associated with city
cityFile = cities[ city ]
# get Web page content
text = getCity( cityFile )
print( text )
main()