String Comparison Testing

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Dec 24 19:55:33 CET 2008


En Wed, 24 Dec 2008 16:20:17 -0200, Brad Causey  
<bcausey at zerodayconsulting.com> escribió:

> I am trying to do some basic log parsing, and well, I am absolutely  
> floored
> at this seemingly simple problem. I am by no means a novice in python,  
> but
> yet this is really stumping me. I have extracted the pertinent code  
> snippets
> and modified them to function as a standalone script. Basically I am  
> reading
> a log file ( in this case, testlog.log) for entries and comparing them to
> entries in a safe list (in this case, safelist.lst). I have spent  
> numerous
> hours doing this several ways and this is the most simple way I can come  
> up
> with:

I'd say the "\n" at the end of each line is the culprit.

> safelist = safelistfh.readlines()

safelist = [line.strip() for line in safelistfh]

(or just strip('\n') if spaces are important); same for the other list.

> def safecheck(line):
>     for entry in safelist:
>         print 'I am searching for\n'
>         print entry
>         print '\n'
>         print 'to exist in\n'
>         print line

repr() is your friend when debugging these things: print repr(entry) etc.

>         comp = line.find(entry)
>         if comp <> -1:
>             out = 'Failed'
>         else:
>             out = 'Passed'

I'd use:
   if entry in line: ...

Are you sure you're interested in the LAST comparison result ONLY?

-- 
Gabriel Genellina




More information about the Python-list mailing list