[Tutor] replace string in a sequentially read stream

János Juhász janos.juhasz at VELUX.com
Mon Sep 12 13:41:25 CEST 2005


Dear Guys,

I have to run the next regexp change in a stream:

import re

wrongcode = re.compile(r'(.*)b24704T')

f = open('testfile.txt')
while 1:
      sequence = f.read(1024)
      sequence = wrongcode.sub(r'\1b24700T', sequence)
      print sequence

My original solution was to keep the last part of the sequence till the
 next sequence hasn't been arrived


while 1:
      try:
            sequence = f.read(1024)
            try:
                  sequence = prevend + sequence
            except:
                  pass
            sequence = wrongcode.sub(r'\1b24700T', sequence)
            prevend = sequence[-20:] # keep the end of the sequence till we
haven't got the complete picture
            sequence = sequence[:-20]     # the first part was checked
correctly
            if not sequence:
                  print prevend
                  break
            print sequence
      except:
            break


In the real sample it works on an IP socket.

Have got someone better idea ?
Probably it is better to work with two sequence.



Best regards,
János



More information about the Tutor mailing list