[Tutor] collecting certain data from a textfile

Olli Virta llvirta at gmail.com
Tue Sep 15 11:34:51 CEST 2009


Hi!

I got this simple but effective piece of code. It picks certain wanted
pieces of data from a textfile based on database records and puts all of
them them in a new textfile in certain order. I was wondering, could it be
possible to make it otherwise in case that there's much more rows in that
textfile and/or there's much more bits of data to collect?

data = open('data.txt','r')
f1 = data.readlines()
data.close()
f2 = open('data.txt','r')
i=0
f3 = []
while i < len(f1):
   a=f2.readline()
   b=f2.readline()
   c=f2.readline()
   data2 = (
            a[1:38].strip()+';'+
            a[54:88].strip()+';'+
            b[77:96].strip()+';'+
            b[1:16].strip()+';'+
            c[23:33].strip()+';'+
            c[123:133].strip()+';'
            )
   wanted = (data2)
   f3.append(wanted + "\n")
   i += 3
f4 = open('wanted.txt', 'w')
f4.write(''.join(f3))
f2.close()
f4.close()
Thanks! OV
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090915/9f2affe8/attachment.htm>


More information about the Tutor mailing list