parsing text from a file

W

Wes James

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.

Also this does not work:

for s in f:
try:
i=s.index('L')
print s[i:i+7]
except:
pass

all it prints is "LAND"

how do I find LANDesk in a string like this. is the "\\" messing things up?

thx,

-wj
 
J

John Machin

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top