Login Register






Challenge (3) Get USA Temperature Extremes filter_list
Author
Message
Challenge (3) Get USA Temperature Extremes #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:
Code:
./ustemp.py [-a/--average] [high/low]
Example:
Code:
./ustemp.py --average low

Code:
#!/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.)

[+] 1 user Likes m0dem's post
Reply

RE: Challenge (3) Get USA Temperature Extremes #2
Well... is anyone even able to complete this challenge? It's pretty easy...

Reply

RE: Challenge (3) Get USA Temperature Extremes #3
Not I. I'm literally only familiar with a couple of python libraries. But I guess I could use these challenges to actually do some outside research and not expect to start coding as soon as I read the challenge.
"If you look for the light, you can often find it. But if you look for the dark, that is all you will ever see.”


[+] 2 users Like Nil's post
Reply

RE: Challenge (3) Get USA Temperature Extremes #4
(12-24-2015, 04:40 AM)m0dem Wrote: Well... is anyone even able to complete this challenge? It's pretty easy...

I'll try it, right after I take a nap. See you in a few hours Biggrin

EDIT: Czerwony Barscz, Uszki, Pierogi, fish and alcohol got the best of me. I swear I'll try this in the near future.
[Image: pBD38Xq.png]
Email: insidious@protonmail.ch

[+] 1 user Likes insidious's post
Reply