Parsing files -- pyparsing to the rescue?

Allan Zhang allanz at gmail.com
Tue Jan 17 01:06:45 CET 2006


Try this

code
=====
import re
p = re.compile(r'<SYSLIB>([^<]*)<')
s = open("file").read()
m = re.search(p, s)
if m: res = m.groups()[0]
res = res.lstrip("\n")
res = res.rstrip("\n")
print res


result:
=======
%python parser.py
Sys Data
Sys-Data
asdkData
Data
%

Thanks
Allan
"rh0dium" <sklass at pointcircle.com> wrote in message 
news:1137445296.573399.60970 at g47g2000cwa.googlegroups.com...
> Hi all,
>
> I have a file which I need to parse and I need to be able to break it
> down by sections.  I know it's possible but I can't seem to figure this
> out.
>
>    The sections are broken by <> with one or more keywords in the <>.
> What I want to do is to be able to pars a particular section of the
> file.  So for example I need to be able to look at the SYSLIB section.
> Presumably the sections are
>
>
> <SYSLIB>
> Sys Data
> Sys-Data
> asdkData
> Data
> <LOGLVS>
> Data
> Data
> Data
> Data
> <SOME SECTION>
> Data
> Data
> Data
> Data
> <NETLIST>
> Data
> Data
> Data
> Data
> <NET>
>
> So if I wanted to break them down..
>
> Sections are broken down by this..
>
> secH=pyparsing.LineStart() + pyparsing.Suppress(
> pyparsing.Literal("<")) +
> pyparsing.OneOrMore(pyparsing.Word(pyparsing.alphanums)) +
> pyparsing.Suppress( pyparsing.Literal(">"))
>
> But how do I say that <SECTIONn> stops at the start of the next
> <SECTIONm>?
> 





More information about the Python-list mailing list