私はまだPythonに非常に慣れていませんが、NOAAから天気を解析し、ラジオ放送の順序で表示するコードを作成しようとしています。
htmlファイルが行のリストに切り刻まれて適切な順序で再出力されるPython式を使用する現在の条件リストをまとめることができましたが、それぞれが1行のデータでした。そのコードは次のようになりました。
#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())
予測部分に取り組んでいるので、問題が発生しています。各予測は必然的に複数の行にまたがって実行され、ファイルを行のリストに切り刻んだからです。
つまり、私が探しているのは、同様のループを使用できるようにする式です。今回は、見つかった行で追加を開始し、&&だけを含む行で終了します。このようなもの:
#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
このコードを改善するにはどうすればよいですか?非常に冗長であることはわかっていますが、始めている間は、自分が何をしていたかを追跡できるのが好きでした。前もって感謝します!