import urllib.request
import re

base_url = "https://www.metoffice.gov.uk/pub/data/weather/uk/climate/stationdata/$data.txt"
places = ["bradford", "braemar", "cambridge", "eastbourne"]


hdr = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' }

max_temps = {}

def get_text(place):   
    place_url = base_url.replace("$",place)

    req = urllib.request.Request(place_url, headers=hdr)
    

    with urllib.request.urlopen(req) as response:
       fulltext = response.read().decode('utf-8')
    
    return(fulltext)

for place in places:
    print("Processing ... " + place)
    
    max_temps[place] = {}

    fulltext = get_text(place)
    
    linenum=0
    for line in fulltext.splitlines():
        
        if linenum>7:

            year_month_TMP = [re.sub('[^0123456789\.]*', '', point) 
                          for point in line.split()]
            year_month = [float(point) for point in year_month_TMP if len(point)]

            if year_month[0] >= 1986 and year_month[1] == 8:
                max_temps[place][int(year_month[0])] = year_month[2]
               
                
            if year_month[0]>2006:
                break
        
        linenum+=1
        
    
    print(max_temps)
Processing ... bradford
{'bradford': {1986: 15.9, 1987: 18.0, 1988: 18.1, 1989: 19.6, 1990: 21.4, 1991: 20.8, 1992: 18.0, 1993: 18.2, 1994: 18.4, 1995: 23.3, 1996: 20.2, 1997: 22.1, 1998: 19.1, 1999: 19.5, 2000: 20.1, 2001: 20.1, 2002: 20.2, 2003: 21.6, 2004: 20.5, 2005: 20.1, 2006: 19.1}}
Processing ... braemar
{'bradford': {1986: 15.9, 1987: 18.0, 1988: 18.1, 1989: 19.6, 1990: 21.4, 1991: 20.8, 1992: 18.0, 1993: 18.2, 1994: 18.4, 1995: 23.3, 1996: 20.2, 1997: 22.1, 1998: 19.1, 1999: 19.5, 2000: 20.1, 2001: 20.1, 2002: 20.2, 2003: 21.6, 2004: 20.5, 2005: 20.1, 2006: 19.1}, 'braemar': {1986: 14.0, 1987: 16.3, 1988: 16.0, 1989: 16.8, 1990: 17.8, 1991: 18.6, 1992: 15.3, 1993: 15.3, 1994: 16.9, 1995: 22.1, 1996: 17.9, 1997: 20.3, 1998: 16.4, 1999: 16.9, 2000: 17.8, 2001: 16.8, 2002: 18.5, 2003: 19.8, 2004: 18.4, 2005: 17.6, 2006: 17.7}}
Processing ... cambridge
{'bradford': {1986: 15.9, 1987: 18.0, 1988: 18.1, 1989: 19.6, 1990: 21.4, 1991: 20.8, 1992: 18.0, 1993: 18.2, 1994: 18.4, 1995: 23.3, 1996: 20.2, 1997: 22.1, 1998: 19.1, 1999: 19.5, 2000: 20.1, 2001: 20.1, 2002: 20.2, 2003: 21.6, 2004: 20.5, 2005: 20.1, 2006: 19.1}, 'braemar': {1986: 14.0, 1987: 16.3, 1988: 16.0, 1989: 16.8, 1990: 17.8, 1991: 18.6, 1992: 15.3, 1993: 15.3, 1994: 16.9, 1995: 22.1, 1996: 17.9, 1997: 20.3, 1998: 16.4, 1999: 16.9, 2000: 17.8, 2001: 16.8, 2002: 18.5, 2003: 19.8, 2004: 18.4, 2005: 17.6, 2006: 17.7}, 'cambridge': {1986: 19.1, 1987: 20.5, 1988: 21.4, 1989: 24.1, 1990: 25.7, 1991: 23.7, 1992: 21.2, 1993: 20.5, 1994: 22.1, 1995: 25.7, 1996: 22.6, 1997: 26.3, 1998: 22.6, 1999: 21.6, 2000: 23.1, 2001: 22.9, 2002: 22.9, 2003: 25.3, 2004: 24.0, 2005: 22.6, 2006: 21.5}}
Processing ... eastbourne
{'bradford': {1986: 15.9, 1987: 18.0, 1988: 18.1, 1989: 19.6, 1990: 21.4, 1991: 20.8, 1992: 18.0, 1993: 18.2, 1994: 18.4, 1995: 23.3, 1996: 20.2, 1997: 22.1, 1998: 19.1, 1999: 19.5, 2000: 20.1, 2001: 20.1, 2002: 20.2, 2003: 21.6, 2004: 20.5, 2005: 20.1, 2006: 19.1}, 'braemar': {1986: 14.0, 1987: 16.3, 1988: 16.0, 1989: 16.8, 1990: 17.8, 1991: 18.6, 1992: 15.3, 1993: 15.3, 1994: 16.9, 1995: 22.1, 1996: 17.9, 1997: 20.3, 1998: 16.4, 1999: 16.9, 2000: 17.8, 2001: 16.8, 2002: 18.5, 2003: 19.8, 2004: 18.4, 2005: 17.6, 2006: 17.7}, 'cambridge': {1986: 19.1, 1987: 20.5, 1988: 21.4, 1989: 24.1, 1990: 25.7, 1991: 23.7, 1992: 21.2, 1993: 20.5, 1994: 22.1, 1995: 25.7, 1996: 22.6, 1997: 26.3, 1998: 22.6, 1999: 21.6, 2000: 23.1, 2001: 22.9, 2002: 22.9, 2003: 25.3, 2004: 24.0, 2005: 22.6, 2006: 21.5}, 'eastbourne': {1986: 18.3, 1987: 20.0, 1988: 19.2, 1989: 21.6, 1990: 22.3, 1991: 21.1, 1992: 19.6, 1993: 19.4, 1994: 20.6, 1995: 23.5, 1996: 20.4, 1997: 22.6, 1998: 21.0, 1999: 20.8, 2000: 21.2, 2001: 21.1, 2002: 21.6, 2003: 22.9, 2004: 21.4, 2005: 21.4, 2006: 20.9}}
for name,place_max in max_temps.items():
    print(name)
    for year, tmax in place_max.items():
        print(str(year) + " : " + str(tmax))
bradford
1986 : 15.9
1987 : 18.0
1988 : 18.1
1989 : 19.6
1990 : 21.4
1991 : 20.8
1992 : 18.0
1993 : 18.2
1994 : 18.4
1995 : 23.3
1996 : 20.2
1997 : 22.1
1998 : 19.1
1999 : 19.5
2000 : 20.1
2001 : 20.1
2002 : 20.2
2003 : 21.6
2004 : 20.5
2005 : 20.1
2006 : 19.1
braemar
1986 : 14.0
1987 : 16.3
1988 : 16.0
1989 : 16.8
1990 : 17.8
1991 : 18.6
1992 : 15.3
1993 : 15.3
1994 : 16.9
1995 : 22.1
1996 : 17.9
1997 : 20.3
1998 : 16.4
1999 : 16.9
2000 : 17.8
2001 : 16.8
2002 : 18.5
2003 : 19.8
2004 : 18.4
2005 : 17.6
2006 : 17.7
cambridge
1986 : 19.1
1987 : 20.5
1988 : 21.4
1989 : 24.1
1990 : 25.7
1991 : 23.7
1992 : 21.2
1993 : 20.5
1994 : 22.1
1995 : 25.7
1996 : 22.6
1997 : 26.3
1998 : 22.6
1999 : 21.6
2000 : 23.1
2001 : 22.9
2002 : 22.9
2003 : 25.3
2004 : 24.0
2005 : 22.6
2006 : 21.5
eastbourne
1986 : 18.3
1987 : 20.0
1988 : 19.2
1989 : 21.6
1990 : 22.3
1991 : 21.1
1992 : 19.6
1993 : 19.4
1994 : 20.6
1995 : 23.5
1996 : 20.4
1997 : 22.6
1998 : 21.0
1999 : 20.8
2000 : 21.2
2001 : 21.1
2002 : 21.6
2003 : 22.9
2004 : 21.4
2005 : 21.4
2006 : 20.9
temps_by_year = {}

for name,place_max in max_temps.items():

    for year, tmax in place_max.items():
        if year not in temps_by_year:
            temps_by_year[year] = {}
            
        temps_by_year[year][name]=tmax
        
print("year" , end=",")
for place in places:
    print(place, end=",")

for year,vals in temps_by_year.items():
    print("")
    print(year, end=",")
    for place in places:
        print(str(vals[place]), end=",")
    
year,bradford,braemar,cambridge,eastbourne,
1986,15.9,14.0,19.1,18.3,
1987,18.0,16.3,20.5,20.0,
1988,18.1,16.0,21.4,19.2,
1989,19.6,16.8,24.1,21.6,
1990,21.4,17.8,25.7,22.3,
1991,20.8,18.6,23.7,21.1,
1992,18.0,15.3,21.2,19.6,
1993,18.2,15.3,20.5,19.4,
1994,18.4,16.9,22.1,20.6,
1995,23.3,22.1,25.7,23.5,
1996,20.2,17.9,22.6,20.4,
1997,22.1,20.3,26.3,22.6,
1998,19.1,16.4,22.6,21.0,
1999,19.5,16.9,21.6,20.8,
2000,20.1,17.8,23.1,21.2,
2001,20.1,16.8,22.9,21.1,
2002,20.2,18.5,22.9,21.6,
2003,21.6,19.8,25.3,22.9,
2004,20.5,18.4,24.0,21.4,
2005,20.1,17.6,22.6,21.4,
2006,19.1,17.7,21.5,20.9,