Challenge (3) Get USA Temperature Extremes 12-22-2015, 06:26 AM
#1
Scrape the temperature extremes of the US states, from this source: (only) https://en.wikipedia.org/wiki/U.S._state...e_extremes
With that data, find the average record high and low of all the states.
Here is my solution, I made it a few months back.
It is a bit more complex than needed (cli args), but it still works.
Usage:
Example:
With that data, find the average record high and low of all the states.
Here is my solution, I made it a few months back.
It is a bit more complex than needed (cli args), but it still works.
Usage:
Code:
./ustemp.py [-a/--average] [high/low]Code:
./ustemp.py --average lowCode:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import argparse
import HTMLParser
import re
import urllib2
US_TEMP_EXTREMES = "https://en.wikipedia.org/wiki/U.S._state_temperature_extremes"
class TableParser(HTMLParser.HTMLParser):
def __init__(self):
HTMLParser.HTMLParser.__init__(self)
self.in_td = False
self.table = []
def handle_starttag(self, tag, attrs):
if tag == "td":
self.in_td = True
def handle_data(self, data):
if self.in_td:
self.table.append(data)
def handle_endtag(self, tag):
self.in_td = False
def getAvg(l):
return sum(l) / len(l)
# extract the first int value from our `data` string
def getFirstInt(data):
tmpData = ""
for char in data:
try:
int(char)
tmpData += char
except ValueError:
if char == "-":
tmpData += char
else:
break
return int(tmpData)
def getColumnData(table, columns, column):
temps = []
for i in range(0, 11):
data = table[i * columns + column]
data = getFirstInt(data)
temps.append(data)
for i in range(11, 50):
data = table[i * columns + column + 1]
data = getFirstInt(data)
temps.append(data)
return temps
# handle the arguments
argparser = argparse.ArgumentParser()
argparser.add_argument("-a", "--average", nargs = 1, default = "high", choices = ["low", "high"])
args = argparser.parse_args()
FLAG = args.average
if type(FLAG) == list:
FLAG = FLAG[0]
# parse the HTML table data
html = urllib2.urlopen(US_TEMP_EXTREMES).read()
parser = TableParser()
parser.feed(html)
table = []
for string in parser.table:
tmpString = ""
dashHex = ["\xe2", "\x88", "\x92"]
c = 0
for i in range(0, len(string)):
nextHex = dashHex[c]
if string[i].isalnum() or string[i] == nextHex:
if string[i] == nextHex:
c += 1
if c >= len(dashHex):
c = 0
tmpString += "-"
else:
c = 0
tmpString += string[i]
if tmpString != "":
table.append(tmpString)
if FLAG == "high":
temps = getColumnData(table, 7, 1)
elif FLAG == "low":
temps = getColumnData(table, 7, 4)
print("The average temperature {} in the US among the 50 states is {} °F".format(FLAG, getAvg(temps)))
(This post was last modified: 12-22-2015, 06:35 AM by m0dem.)







![[+]](https://sinister.li/images/modern/collapse_collapsed.png)








![[Image: pBD38Xq.png]](http://i.imgur.com/pBD38Xq.png)