Olli Virta wrote:
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?

Otherwise is always possible! What is the problem? Why do you want it "otherwise"?

data = open('data.txt','r')
f1 = data.readlines()
data.close()

There is no need to do the above! You don't need a line count to control the loop.

f2 = open('data.txt','r')

Drop next lline:

i=0
f3 = []

Change next two lines:

while i < len(f1):
   a=f2.readline()

To:

while True:
  a=f2.readline()
  if not a: break

   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)

If you are concerned about enough memory, write data2 to the file instead of collecting in a list.

   f3.append(wanted + "\n")

Drop next line:

i += 3 f4 = open('wanted.txt', 'w')
f4.write(''.join(f3))
f2.close()
f4.close()


--
Bob Gailer
Chapel Hill NC
919-636-4239
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to