Newbie question : string processing with RE

Patrick.Bussi at space.alcatel.fr Patrick.Bussi at space.alcatel.fr
Tue Mar 27 10:32:20 EST 2001


Hello,

I have difficulty with the search method of the re module, when applied to an
object retrieved from urllib.In the following program, f3 is read once and the
search is effective. But when I try to read it again, it acts as if the index of
a file had to be "rewinded" (e.g. as would do a "seek" on a "true" file). I
thought that f3.read was to be considered as a string. Could someone help me
understand my mistake and give me a nice solution (means not storing data into a
file).

Thank you in advance.


 #! /usr/bin/env python
 # file test 22
 import re, socket, urllib
 encoded_data = ... # bla-bla-bla
 f3 = urllib.urlopen ("www.such-a-nice-webpage.com/index.html", encoded_data)
 p=re.compile("window.open")
 q=re.compile("document.location =" )
 print p,'\n',q
 t1=p.search(f3.read())
 t2=q.search(f3.read())
 print t1.group()
 print t2.group()
 # ---- end of file test22 ----


 $ test22
 <re.RegexObject instance at 8135458>
 <re.RegexObject instance at 81354e0>
 window.open
 Traceback (innermost last):
   File "./test22", line 12, in ?
     print t2.group()
 AttributeError: 'None' object has no attribute 'group'


---
Patrick Bussi
patrick.bussi at space.alcatel.fr


Any opinions expressed are my own and not necessarily those of my Company.






More information about the Python-list mailing list