Pyparsing...

Raoul raoulsam at yahoo.com
Tue Sep 21 02:39:24 CEST 2004


I am futzing with pyparsing and for the most part enjoying it.
However, I'm running into trouble with whitespace delimited lists. I
get data in blocks like this:

[QC1]
Type=15
NumberCells=1925
CellHeader=X	Y	PROBE	PLEN	ATOM	INDEX
Cell1=132	0	N	25	0	132
Cell2=652	0	N	25	0	652
Cell3=648	0	N	25	0	648
...

I'd like to be able to parse this structure.

Ideally, I'd like for a QC node, to have a dictionary with
{'number':1
 'type' : 15
 'NumberCells' : 1925
 'Table' : [{'cell':1,'x':132,'y':0,'probe':25,'plen':0,'atom':132',
'index':None}, {'cell':2 ....

I'm running into the following problems:

1. I can't seem to use delimitList() to define a rule that parses the
right hand side of the table into
['x','y','probe','plen','atom','index']. I think it's because my lists
are whitespace delimited.

2. I can't seem to convert value into an integer, for example, I can
parse each row in the table to :
  ['Cell','2','=', '652	0	N	25	0	652']
but am unable to get the setParseAction(see below) to convert and
substitute in the right value.

Any hints will help a great deal. Thanks...

Raoul-Sam


 I have some ugly non functional code below.. 

def cdffile_BNF():
    global cdfbnf 

    if not cdfbnf:
        makeint = Word(nums).setParseAction( lambda s,l,t:[int(t[0])])
        equals = Literal("=").suppress()
        nonequals = "".join( [ c for c in printables if c != "=" ] ) +
" \t"

        key = Word(nonequals)
        value = Word(nonequals)
        kvp = Group(key + equals + restOfLine)
        kvpBlk = OneOrMore(kvp)

        headerCell = delimitedList(Word(alphanums)," ")
        rowHeader = Combine( Literal("CellHeader") + equals +
headerCell)
        row = Combine(Literal("Cell").suppress() + restOfLine)
        rows = OneOrMore(row)

        CDF = Literal("[CDF]")
        CDFBlk = Group(CDF + kvpBlk)
        
        CHIP = Literal("[CHIP]")
        CHIPBlk = Group(CHIP + kvpBlk)
        CHIPBlk.setResultsName("chip")

        QC = Combine( Literal("[QC").suppress() + Word(nums) +
Literal("]").suppress())
        QCBlk = QC + kvp + kvp + rowHeader + rows
        
        cdfbnf = CDFBlk + CHIPBlk + QCBlk
                
    return cdfbnf



More information about the Python-list mailing list