[Tutor] read file and match string

Kalle Svensson kalle@gnupung.net
Fri, 1 Feb 2002 01:13:07 +0100

[Jim Ragsdale]
> Ok, im searching a log file from an Unreal Tournament server. it generates
> some output, but im really intested in 1 line:
> ScriptLog: [PLAYER_JOIN] {RS}]V{AD_l)OG_3 ip.address.omitted:1040 2600
> Each line will have ScriptLog: [PLAYER_JOIN] a name and ip:port and another
> number. The other lines are just different server events and other things of
> interest. i just want the ip address.

I see.  That's a very nice project to get started with a language.  To
me, the format suggests the use of string methods.  I suppose there
are no other lines that contain "[PLAYER_JOIN]"?  And there is always
the same number of spaces on each interesting line?  In that cas, you
could try using something like:

in = open("file.in")
out = open("file.out", "w")
for line in in.xreadlines():
    if line.find("[PLAYER_JOIN]"):
        ip_and_port = line.split()[3]
        out.write(ip_and_port + "\n")

With the example line above, this would write
to the output file.  You can easily get rid of the port number by
ip = ip_and_port.split(":")[0]
or, if the port number is always four digits
ip = ip_and_port[:-5]

I don't know if this will be any faster, but I guess it doesn't matter
that much.  0.4 seconds is probably fast enough?

> I did the regular expression because that is what i found on the net. Messed
> w/ it until i got it to match :)  And I just upgraded to Python2.2.  Using
> xreadline on a 1.56meg file on my 400celeron laptop takes about .4 sec.( Ran
> start = time.clock() before and finish = time.clock() after and took the
> difference) Also used the localtime function to write a date/time stamp in
> the logfile name(iplog.1.31.2002.5.35.log).
> Right now im trying to clean it up, use functions and stuff. do it right :)
> I would like to add some functionality as i go along. Say maybe determine if
> the log file has been reset and if it hasnt, start from where it left off
> last time. If it has been restarted, start fresh and maybe start a new
> output file.

Sounds very nice!  When you get it ready, I suggest you submit it to
"Useless Python", a web site collecting python code snippets, tips and
challenges managed by a fellow tutor list member.  And it's not all
useless... :)  The address is http://www.lowerstandard.com/python/ .

Kalle Svensson (kalle@gnupung.net) - Laziness, impatience, hubris: Pick two!
English: http://www.gnupung.net/  Svenska: http://www.lysator.liu.se/~kalle/
Stuff: ["http://www.%s.org/" % x for x in "gnu debian python emacs".split()]