parsing text from a file

John Machin sjmachin at lexicon.net
Thu Jan 29 17:34:36 EST 2009


On Jan 30, 8:54 am, Wes James <compte... at gmail.com> wrote:
> If I read a windows registry file with a line like this:
>
> "{C15039B5-C47C-47BD-A698-A462F4148F52}"="v2.0|Action=Allow|Active=TRUE|Dir=In|Protocol=6|Profile=Public|App=C:\\Program
> Files\\LANDesk\\LDClient\\tmcsvc.exe|Name=LANDesk Targeted
> Multicast|Edge=FALSE|"
>
> with this code:
>
> f=open('fwrules.reg2.txt')
>
> for s in f:
>   if s.find('LANDesk') <0:
>     print s,
>
> LANDesk is not found.

You mean it's not printed. That code prints all lines that don't
contain "LANDesk"

>
> Also this does not work:
>
> for s in f:
>   try:
>     i=s.index('L')
>     print s[i:i+7]
>  except:

Using "except ValueError:" would be safer.

>    pass
>
> all it prints is "LAND"


AFAICT your reported outcome is impossible given that such a line
exists in the file.
>
> how do I find LANDesk in a string like this.

What you were trying (second time, or first time (with >=) should
work. I suggest that to diagnose your problem you change the second
snippet as follows:
1. use except ValueError:
2. print s, len(s), i, and s.find('L') for all lines

>  is the "\\" messing things up?

Each "\\" is presumably just the repr() of a single backslash. In any
case whether there are 0,1,2 or many backslashes in a line or the repr
() thereof has nothing to do with your problem.

HTH,
John



More information about the Python-list mailing list