I'm still incredibly new at Python, but am trying to write code that will parse the weather from NOAA and display it in the order of our radio broadcast.
I've managed to put together a current conditions list that uses a python expression where the html file gets chopped up into a list of lines, and then is re-output in the proper order, but each of those was a single line of data. That code looked like this:
#other function downloads
#http://www.arh.noaa.gov/wmofcst_pf.php?wmo=ASAK48PAFC&type=public
#and renames it currents.html
from bs4 import BeautifulSoup as bs
import re
soup = bs(open('currents.html')
weatherRaw = soup.pre.string
towns = ['PAOM', 'PAUN', 'PAGM', 'PASA']
townOut = []
weatherLines = weatherRaw.splitlines()
for i in range(len(towns)):
p = re.compile(towns[i] + '.*')
for line in weatherLines:
matched = p.match(line)
if matched:
townOut.append(matched.group())
Now that I'm working on the forecast portion, I'm running into a problem, since each forecast necessarily runs over multiple lines, and I've chopped the file into a list of lines.
So: what I'm looking for is an expression that will allow me to use a similar loop, this time starting the append at the line found and ending it at a line containing just &&. Something like this:
#sample data from http://www.arh.noaa.gov/wmofcst.php?wmo=FPAK52PAFG&type=public
#BeautifulSouped into list fcst (forecast.pre.get_text().splitlines())
zones = ['AKZ214', 'AKZ215', 'AKZ213'] #note the out-of-numerical-order zones
weatherFull = []
for i in range(len(zones)):
start = re.compile(zones[i] '.*')
end = re.compile('&&')
for line in fcst:
matched = start.match(line)
if matched:
weatherFull.append(matched.group())
#and the other lines of various contents and length
#until reaching the end match object
What should I do to improve this code? I know it's very verbose, but while I'm starting out, I liked to be able to track what I was doing. Thanks in advance!