anything wrong with using pyparsing
i've written this to parse my own .dotf file:
def parseFromDOTF(file_path):
comment = "%" + restOfLine
typeToken = CaselessKeyword("@TYPE")
attrToken = CaselessKeywo开发者_运维百科rd('@ATTRIBUTE')
ident = Word(alphas,alphanums)
type = Suppress(typeToken) + ident
columnList = Group(delimitedList(nums))
attribute = Group(Suppress(attrToken) + ident("attribute") + columnList("column"))
DOTF = type('type') + OneOrMore(attribute)("attributes")
DOTF.ignore(comment)
return DOTF.parseFile(file_path)
below is a sample of .dotf file
%a comment
@TYPE NORMAL
@ATTRIBUTE id 0
@ATTRIBUTE values 1,2,3,4
@ATTRIBUTE class 5
but there is something wrong with it:
pyparsing.ParseException: Expected "0123456789" (at char 79), (line:3, col:15)
the 15th col of line 3 is a whitespace, isn't it?
so ,what's wrong?
thanks!
nums
equals the string '0123456789'
.
So the definition
columnList = Group(delimitedList(nums))
tells pyparsing that a columnList
should be a comma-delimited list of strings,
with each string literally being '0123456789'
.
To instead match a comma-delimited list of "words" composed of characters from the string nums
, use Word(nums)
:
integer = Word(nums)
columnList = Group(delimitedList(integer))
精彩评论