0

当我将机场设置为一个变量时,我可以获得文本文件。但是,如何获取多个机场代码的文本文件并显示信息?

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
4

2 回答 2

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 汽车 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

于 2012-10-30T08:27:13.887 回答
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(', ')`
于 2012-10-30T08:26:16.337 回答