All pastes #881346 Raw Edit

icao-parse.py

public python v1 · immutable
#881346 ·published 2008-01-29 22:54 UTC
rendered paste body
import urllib2, copyICAO = "EGLL" # ICAO code we're looking forsock = urllib2.urlopen("http://adds.aviationweather.gov/metars/stations.txt")airport_list = sock.read()sock.close()airport_list = airport_list.splitlines()# filter out comments# this is broke and even if it worked there's more efficient waysfor line in airport_list:	if line.startswith("!"):		airport_list.remove(line)# fun time# this does semi work but again, there's more efficient waysfor a_index,line in enumerate(airport_list): # loop through each line	airport_icao = None # stupid python.... (don't ask)	icao_index = None # ^	if len(line.split()) < 10: # 10 is a high guesstimate, it's to strip out the lines like "OMAN US-MIL        08-APR-03"		continue	for c_index,c in enumerate(line.split()): # loop through each space seperated segment of the line		if len(c) == 4 and c.isupper() and c.isalpha(): # check it looks like an ICAO				airport_icao = c				icao_index = c_index	if airport_icao == ICAO: # check it's the ICAO we're looking for		print "ICAO: %s -- Name: %s" % (airport_icao, " ".join(airport_list[a_index].split()[0:icao_index]))		break # end the first loop now since there's no point going on