extracting numbers from a file, excluding fixed words

Kent Johnson kent37 at tds.net
Sat Oct 29 20:49:33 CEST 2005


dawenliu wrote:
> Hi, I have a file with this content:
> xxxxxxx xxxxxxxxxx xxxxx xxxxxxx
> 1
> 0
> 0
> 0
> 1
> 1
> 0
> (many more 1's and 0's to follow)
> yyyyy yyyyyy yyy yyyyyy yyyyy yyy
> 
> The x's and y's are FIXED and known words which I will ignore, such as
> "This is the start of the file" and "This is the end of the file".  The
> digits 1 and 0 have UNKNOWN length.  I want to extract the digits and
> store them in a file.  Any suggestions will be appreciated.
> 

Off the top of my head (not tested):

inf = open('input.txt')
out = open('output.txt', 'w')

skips = [
  'xxxxxxx xxxxxxxxxx xxxxx xxxxxxx',
  'yyyyy yyyyyy yyy yyyyyy yyyyy yyy',
]

for line in inf:
  for skip in skips:
    if skip in line:
      continue
  out.write(line)

inf.close()
out.close()

Kent



More information about the Python-list mailing list