当我将机场设置为一个变量时,我可以获取文本文件。但是,如何获取多个机场代码的文本文件并显示信息?
airport = 'KSFO, KSJC, KOAK'
for metar in urlopen('http://weather.noaa.gov/pub/data/observations/metar/stations/%s.TXT' %airport):
metar = metar.decode("utf-8")
if "%s" %airport in metar:
print metar
答案 0 :(得分:2)
如果您的目标是获取每个机场的天气观测结果,您可以使用:
from urllib import urlopen
airports = 'KSFO, KSJC, KOAK'
for airport_code in airports.split(","):
for metar in urlopen('http://weather.noaa.gov/pub/data/observations/metar/stations/%s.TXT' % airport_code.strip()):
metar = metar.decode("utf-8")
print metar
对我来说,输出是:
2012/10/30 07:56 KSFO 300756Z 29005KT 10SM FEW001 13/11 A3006 RMK AO2 SLP178 T01280111 402110117
2012/10/30 07:53 KSJC 300753Z AUTO 00000KT 10SM CLR 10 / A3005 RMK AO2 SLP175 T0100 402060089 $
2012/10/30 08:14 KOAK 300814Z 06003KT 10SM OVC004 13/12 A3007 RMK AO2
答案 1 :(得分:1)
airport = 'KSFO, KSJC, KOAK'
for airports in airport.split(', '):
for metar in urlopen('http://weather.noaa.gov/pub/data/observations/metar/stations/%s.TXT' %airports):
metar = metar.decode("utf-8")
if "%s" %airports in metar:
print metar
基本上,我们将airport
变量分隔为三个不同的变量,其中,
(逗号,然后是空格)是分隔符:
`airport.split(', ')`