CSC111 How cold is it in England?

From dftwiki3
Revision as of 09:55, 13 December 2011 by Thiebaut (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut 22:57, 12 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 getCity( 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 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:
            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()




...