Newbie array question

TomH beans at bedford.net
Sun Jun 13 14:45:40 CEST 2004


nhirsh2 at ieee.org (Neale) wrote in message news:<2d7af4f8.0406112032.4cb4438a at posting.google.com>...
> My first task with Python is to scan multiple small text files and
> "batch together" those records with a "5" or "6" in column 2, and then
> save as a new file. The records are 256 characters. I know it sounds
> like a homework problem, but it's not.
> 
> My math brain wants to dump all the records into a big 2-D array and
> sort on the second column. Then "compress out" all the records whose
> second character isn't "5" or "6". Easy to say.
> 
> Is this the right strategy for using Python? Does it even have a
> "compress out" function?

I must be missing something. Isn't this simply: read the records and
write the ones you want:

outf = open('of', 'w')
for f in ['a','b']:
  d = open(f, 'r')
  for ln in d:
    if ln[1] == '5' or ln[0] == '6':
       outf.write (ln)



More information about the Python-list mailing list