parsing text from a file
MRAB
google at mrabarnett.plus.com
Thu Jan 29 18:50:08 EST 2009
Wes James 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.
>
> 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?
>
How do you know what's in the file? Did you use an editor? It might be
that the file contents are encoded in, say, UTF-16 and the editor is
detecting that and decoding it for you, but Python's open() function is
just returning the contents as a bytestring (Python 2.x).
Try:
import codecs
f = codecs.open('fwrules.reg2.txt', encoding='UTF-16')
for s in f:
if u'LANDesk' in s:
print s,
f.close()
More information about the Python-list
mailing list