sys.stdin and re.search

John Machin sjmachin at lexicon.net
Mon Jan 20 06:09:12 EST 2003


"Jimmie Fulton" <jimmie-dated-1043474834.7879b0 at illumid.com> wrote in message news:<pan.2003.01.20.06.24.23.588538 at illumid.com>...
> I'm unable to get any matches with re in a loop when the file I'm looping
> over is stdin.  When I use open(), it works fine.  Any hints?
> 
> Sample Non-working code...

This works for me. What version of Python are you using? What platform
[at least specify (*x / Mac / Windows)]? Are you piping into stdin or
are you typing?

I suggest that you insert this statement immediately after the "while
line:":
   print "'line' is", repr(line)
That way you can verify that you are actually getting input, and you
can see what the search() has to work on.

> pattern = re.compile(r"(\b[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\b)")

You don't need those square brackets, it will still work without them.

> Sample Working code...
> file = open('myfile','r')

Not a good trick on Python 2.2 (2.1??) or later; you have just nobbled
the built-in file() function.

Oh, and by the way, did you by some mischance actually have stdout
instead of stdin in your code??? sys.stdout.readline() returns ''
immediately and you would get the symptoms you describe.




More information about the Python-list mailing list