Re: [Pyparsing] pyparsing svg - noob
Brought to you by:
ptmcg
From: Paul M. <pa...@al...> - 2007-11-08 15:01:59
|
Donn - 1. Here is why you are having trouble getting floats out of a parsed string: float = ... whatever ... You are redefining the global float! Change float = Combine(Optional("-") + Word(nums) + dot + Word(nums)) to float_ = Combine(Optional("-") + Word(nums) + dot + Word(nums)) or realNum = Combine(Optional("-") + Word(nums) + dot + Word(nums)) or floatValue = Combine(Optional("-") + Word(nums) + dot + Word(nums)) Call it anything but float! 2. It seems the optional closing "Z" is being ignored. dot = Literal(".") float = Combine(Optional("-") + Word(nums) + dot + Word(nums)) command = oneOf("M L C Z") comma = Literal(",").suppress() couple = Group(float + comma + float) phrase = OneOrMore(command + Group(OneOrMore(couple)) ) Well this is because a phrase is made up of a repetition of (command + couple...), and there are no couples after the 'Z'. You later wrote: The ideal grouping would be something like this (I mix C and L commands) ['M', [float,float], 'C',[float,float,float,float,float,float], 'L',[float,float], 'Z'] Well why didn't you say so? :) In that case, Ralph Corderoy's suggestion works better: M_command = "M" + Group(couple) C_command = "C" + Group(couple + couple + couple) L_command = "L" + Group(couple) Z_command = "Z" command = M_command | C_command | L_command | Z_command phrase = OneOrMore(command) # what would generate your preferred list phrase = OneOrMore(Group(command)) # what I would recommend instead My recommended version of phrase makes each command its own nested list, like: [['M', [float,float]], ['C',[float,float,float,float,float,float]], ['L',[float,float]], ['Z']] Now you *can* iterate cleanly over this list, since the variable-length couple lists are enclosed in a list with their respective command letter. for command,couples in tokens[:-1]: print command print couples.asList() -- Paul |