Please always reply-all so a copy goes to the Tutor list.
Olli Virta wrote:
Hi! Thanks for advice. I was thinking, if is there was other ways to
get the wanted data out, than with that strip() method. Say what if
I had to use it hundred times.
Are you concerned with ease of coding or with performance. We usually
don't worry about performance until in practice the program takes "too
long".
Regarding coding:
create tuples of slices:
as = (slice(1,38), slice(54,88))
bs = (slice(77,96), slice(1,16))
cs = (slice(23,33), slice123,133))
slices = (as, bs, cs)
create a tuple of lines: (untested)
a=f2.readline()
if not a: break
b=f2.readline()
c=f2.readline()
lines = (a,b,c)
iterate over the lines and their respective slices:
data2 = []
for x in range(len(lines)):
for s in slices[x]:
data2.append(lines[x][s].strip()
OV
2009/9/15 bob gailer <bgai...@gmail.com <mailto:bgai...@gmail.com>>
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
--
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