parsing from a position found with a regexp

sismex01 at hebmex.com sismex01 at hebmex.com
Wed Sep 25 11:35:39 EDT 2002


> 
> Hi,
> 
> I would like to parse only a portion of a particular
> file. The portion I am interested in starts after the
> last occurrence of a particular string in the file.
> Ideally, I was hoping there might be a function
> (perhaps like the string rfind function, but for
> files) that would make this quick and simple. Any
> suggestions?
> 
> Thanks,
> Mark
> 

Dealing with text files, in a random-access fashion, is
never "quick and simple" :-(

But, something like the following might help?

def file_rfind(fp, string):
   last_pos = -1
   pos = fp.tell()
   for line in fp:
      if line.find(string) > -1: last_pos = pos
      pos += len(line)
   return last_pos


I haven't tested it though, but the final solution won't
be too far away, conceptually. :-/  AND, remember that
text files in M$ operating systems are 2-chars, and on
unix and mac-os based systems are 1-char, and this can
cause problems when opening files in text mode.

Good luck :-)

-gustavo





Advertencia: 
La informacion contenida en este mensaje es confidencial y restringida y
esta destinada unicamente para el uso de la persona arriba indicada, Esta
comunicacion representa la opinion personal del remitente y no refleja
necesariamente la opinion de la Compañia. Se le notifica que esta
estrictamente prohibida cualquier difusion, distribucion o copia de este
mensaje. Si ha recibido esta comunicacion o copia de este mensaje por error,
o si hay problemas en la transmision, favor de comunicarse con el remitente.


Todo el correo electrónico enviado para o desde esta dirección será
procesado por el sistema de correo corporativo de HEB. Tal correo
electrónico esta sujeto a ser almacenado y puede ser revisado por alguien
ajeno al recipiente autorizado con el propósito de monitorear que se cumplan
las normas de seguridad de la empresa.




More information about the Python-list mailing list