Difference between revisions of "CSC111 How cold is it in England?"
(Created page with "--~~~~ ---- <source lang="python"> # ukTemperatures.py # D. Thiebaut # this code is undocumented and possibly not robust... import urllib.request import sys URL = "http://cs.s...") |
|||
(6 intermediate revisions by the same user not shown) | |||
Line 6: | Line 6: | ||
# D. Thiebaut | # D. Thiebaut | ||
# this code is undocumented and possibly not robust... | # this code is undocumented and possibly not robust... | ||
− | + | # Main site: http://www.metoffice.gov.uk/climate/uk/stationdata/ | |
+ | # | ||
import urllib.request | import urllib.request | ||
import sys | import sys | ||
URL = "http://cs.smith.edu/~111a/climate/uk/stationdata/" | 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', | cities = {'Ross-on-Wye' : 'rossonwyedata.txt', | ||
Line 25: | Line 27: | ||
'Sheffield' : 'sheffielddata.txt' } | 'Sheffield' : 'sheffielddata.txt' } | ||
− | def getCity( | + | def getCity( fileName ): |
global URL | global URL | ||
− | print( "Getting list of temperatures from ", | + | print( "Getting list of temperatures from ", URL ) |
− | f = urllib.request.urlopen( URL + | + | f = urllib.request.urlopen( URL + fileName ) |
bytes = f.read() | bytes = f.read() | ||
htmlText = bytes.decode( "utf8" ) | htmlText = bytes.decode( "utf8" ) | ||
Line 44: | Line 46: | ||
while True: | while True: | ||
city = input( "> " ) | city = input( "> " ) | ||
− | if city in cities | + | if city in cities: |
break | break | ||
print( "Sorry,", city,"is not a valid city name" ) | print( "Sorry,", city,"is not a valid city name" ) | ||
Line 61: | Line 63: | ||
<br /> | <br /> | ||
<onlydft> | <onlydft> | ||
+ | =Alternate URL= | ||
+ | |||
+ | * http://cs.smith.edu/dftwiki/DFT/climate/uk/stationdata/ | ||
+ | |||
+ | =Better Developed Program= | ||
<source lang="python"> | <source lang="python"> | ||
import urllib.request | import urllib.request | ||
Line 145: | Line 152: | ||
</source> | </source> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | |||
+ | |||
+ | |||
</onlydft> | </onlydft> | ||
<br /> | <br /> |
Latest revision as of 09:55, 13 December 2011
--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()