import numpy as np import glob list_dat = [] def parse_snd(filename): #icao,iata,synop,name,state,country,lat,lon,elev,priority,srcid ## split the file on newline characters ## and then split each line on whitespace ## and remove the empty strings file = open(filename) lines = file.readlines() for i in range(0, len(lines)): lines[i] = filter(None, lines[i].strip("\n").split(" ")) ## get the meta information for the site time = lines[2][-1] stid = lines[2][2] slon = lines[3][5] slat = lines[3][2] selv = lines[3][-1] write_csv("new_mpas.csv", name=stid, lat=slat, lon=slon, elev=selv, srcid=stid) def write_csv(outfile, **kwargs): icao = kwargs.get("icao", "") iata = kwargs.get("iata", "") synop = kwargs.get("synop", "") name = kwargs.get("name", "") state = kwargs.get("state", "") country = kwargs.get("country", "") lat = kwargs.get("lat", "") lon = kwargs.get("lon", "") elev = kwargs.get("elev", "") priority = kwargs.get("priority", "") srcid = kwargs.get("srcid", "") list_dat.append(icao + "," + iata + "," + synop + "," + name + "," + state + "," + country + "," + lat + "," + lon + "," + elev + "," + priority + "," + srcid + "\n") if __name__ == "__main__": sndfiles = glob.glob("./2016050300/*.snd") outfile = open("new_mpas.csv", "a+") for file in sndfiles: parse_snd(file) outfile.write("icao,iata,synop,name,state,country,lat,lon,elev,priority,srcid\n") list_dat = set(list_dat) for line in list_dat: outfile.write(line) outfile.close()