[Tutor] read text file in zip archive, process, plot

John W washakie at gmail.com
Sun Apr 15 14:32:44 CEST 2007


Kent and Alan: better?
.j

import zipfile
import os
import pylab as P
iFile = raw_input("Which file to process?")

def openarchive(filename):
    """ open the cmet archive and read contents of file into memory """
    z = zipfile.ZipFile(filename, "r")
    for filename in z.namelist():
        print filename
        contents = z.read(filename)
        data = contents.split('\n')
        return data

def plotflight(data):
    """ Plot flight data t vs. hPa with Pylab """
    firstline = data[0].strip().split(' ')
    stind = int(firstline[0])
    hdrline = stind - 1
    x = []
    t = []
    for l in data[stind:-1]:
            l = l.split()  # Split on whitespace
            tval = float(l[0])
            t.append(tval)
            xval = float(l[24])
            x.append(xval)
    #Create scatter plot using pylab function
    P.scatter(t,x)
    P.xlabel('time (s) after 00 UTC')
    P.ylabel('Altitude (hPa)')
    P.title('Flight elevation vs. Time')
    P.grid(True)
    #print out figure
    if os.path.exists("flight_track.png"):
     os.remove("flight_track.png")
    P.savefig('flight_track')
    P.close()

flightdata = openarchive(iFile)
plotflight(flightdata)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070415/b4f3c53b/attachment.htm 


More information about the Tutor mailing list