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

Alan Gauld alan.gauld at btinternet.com
Sun Apr 15 09:35:11 CEST 2007


"Washakie Wyoming" <washakie at juno.com> wrote

> I'm writing to ask for ways to improve it. I feel like the
> writing and reading from 'jnk' files is definitely a hack!

Yup!
I'm not sure why you do it, at least the first time.
You read the file into memory, why not just process it there?
You can split the file into lines using split('\n') and you get
the same list that readlines generates from the jnk file.


So you can replace all of this:

        f = open('jnk','w')
        f.write(contents)
        f.close()
        print "file closed, now reading"
        #now open files to process
        f = open('jnk','r')
        fn = open('newjnk','w')
        data = f.readlines()

with

       data = contents.split('\n')
       fn = open('newjnk','w')

I'm not familiar with pyLab so I don;t know if you can
bypass the newjnk file.... Also you seem to overwrite
the newjnk file before you use it. open('w') will overwrite
the existing file. Is that what you want? Or should you
be appending the data? Or maybe you want to create
a new file for each file in the zipfile? So you create a
new filename like:

       fn = open(filename + '.jnk', 'w')

Othewise your pylab section only ever processes the
last file in the zipfile.

> Any suggestions?

The other thing is that I'd introduce a couple of functions,
one to process each file from the archive, the second to
do the pylab stuff. So your outer loop will shrink to
something like:

for filename in z.namelist():
     prepareFile(filename)
     display(filename)

It helps to keep the logic separated which makes
maintenance easier in the future.

HTH,

Alan G.

Here's my program (a zip is attached - if that's allowed???):

import zipfile
import os
import pylab as P

z = zipfile.ZipFile("flight_data.zip", "r")
for filename in z.namelist():
        print filename
        contents = z.read(filename)
        f = open('jnk','w')
        f.write(contents)
        f.close()
        print "file closed, now reading"
        #now open files to process
        f = open('jnk','r')
        fn = open('newjnk','w')
        data = f.readlines()
        firstline = data[0].strip().split(' ')
        stind = int(firstline[0])
        hdrline = stind - 1
        #print data[stind:len(data)]
        for l in data[stind:len(data)]:
                #l = l.replace('  ',',')
                fn.writelines(l)
        f.close()
        fn.close()
        #print 'file closed now'

jnk2 = P.load('newjnk')
t = jnk2[:,0]
x = jnk2[:,24]

P.scatter(t,x)
P.xlabel('time (s) after 00 UTC')
P.ylabel('Altitude (hPa)')
P.title('Flight elevation vs. Time')
P.grid(True)
if os.path.exists("flight_track.png"):
     os.remove("flight_track.png")
P.savefig('flight_track')
P.close()
os.remove("jnk")
os.remove("newjnk")






________________________________________________________________________
Interested in getting caught up on today's news?
Click here to checkout USA TODAY Headlines.
http://track.juno.com/s/lc?s=198954&u=http://www.usatoday.com/news/front.htm?csp=24




--------------------------------------------------------------------------------


> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 




More information about the Tutor mailing list