使用pyparsing有什么问题吗

2024-09-29 00:16:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我写这个来解析我自己的.dotf文件:

def parseFromDOTF(file_path):
    comment = "%" + restOfLine
    typeToken = CaselessKeyword("@TYPE")
    attrToken = CaselessKeyword('@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)

下面是.dotf文件的示例

^{pr2}$

但它有问题:

pyparsing.ParseException: Expected "0123456789" (at char 79), (line:3, col:15)

第3行的第15列是空白,不是吗?在

那么,怎么了?在

谢谢!在


Tags: 文件pathtypegroupcommentattributefileident
1条回答
网友
1楼 · 发布于 2024-09-29 00:16:02

nums等于字符串'0123456789'。 那么定义呢

columnList = Group(delimitedList(nums))

告诉pyparsing columnList应该是逗号分隔的字符串列表, 每个字符串都是'0123456789'。在

要匹配由字符串nums中的字符组成的逗号分隔的“单词”列表,请使用Word(nums)

^{pr2}$

相关问题 更多 >