Skip to main content
 首页 » 编程设计

python之使用 Pyparse 将多个项目解析并组合在一起

2025年05月04日8davidwang456

这是在 Build a simple parser that is able to parse different date formats using PyParse 上构建的

我有一个解析器可以将一个或多个用户分组到一个列表中 所以 a.parser('show abc, xyz commits from "Jan 10,2015"to "27/1/2015"') 应该将两个用户名分组到列表 [abc,xyz]

对于我拥有的用户:

keywords = ["select", "show", "team", "from", "to", "commits", "and", "or"] 
    [select, show, team, _from, _to,  commits, _and, _or] = [ CaselessKeyword(word) for word in keywords ] 
 
user = Word(alphas+"."+alphas) 
user2 = Combine(user + "'s") 
users = OneOrMore((user|user2)) 

语法是

bnf = (show|select)+Group(users).setResultsName("users")+Optional(team)+(commits).setResultsName("stats")\ 
    +Optional(_from + quotedString.setParseAction(removeQuotes)('from') +\ 
    _to + quotedString.setParseAction(removeQuotes)('to')) 

这是错误的。谁能指导我正确的方向。 另外,在 pyparse 中有没有一种方法可以有选择地决定单词应该属于哪个组。我的意思是“xyz”独立版应该在我的用户列表中。但是“xyz 团队”应该在团队列表下。如果提供了可选的关键字 team ,那么 pyparse 应该以不同的方式对其进行分组。

我无法在网上找到我要找的东西。或者我没有在 Google 上正确地构建我的问题?

请您参考如下方法:

您走在正确的轨道上,请查看此解析器更新中嵌入的评论:

from pyparsing import * 
 
keywords = ["select", "show", "team", "from", "to", "commits", "and", "or"] 
[select, show, team, _from, _to,  commits, _and, _or] = [ CaselessKeyword(word) for word in keywords ] 
 
# define an expression to prevent matching keywords as user names - used below in users expression 
keyword = MatchFirst(map(CaselessKeyword, keywords)) 
 
user = Word(alphas+"."+alphas)  # ??? what are you trying to define here? 
user2 = Combine(user + "'s") 
# must not confuse keywords like commit with usernames - and use ungroup to  
# unpack single-element token lists 
users = ungroup(~keyword + (user|user2)) 
 
#~ bnf = (show|select)+Group(users).setResultsName("users")+Optional(team)+(commits).setResultsName("stats") \ 
    #~ + Optional(_from + quotedString.setParseAction(removeQuotes)('from') + 
                    #~ _to + quotedString.setParseAction(removeQuotes)('to')) 
 
def convertToDatetime(tokens): 
    # change this code to do your additional parsing/conversion to a Python datetime 
    return tokens[0]  
timestamp = quotedString.setParseAction(removeQuotes, convertToDatetime) 
 
# similar to your expression 
# - use delimitedList instead of OneOrMore to handle comma-separated list of items 
# - add distinction of "xxx team" vs "xxx" 
# - dropped expr.setResultsName("name") in favor of short notation expr("name") 
# - results names with trailing '*' will accumulate like elements into a single 
#   named result (short notation for setResultsName(name, listAllValues=True) ) 
# - dropped setResultsName("stats") on keyword "commits", no point to this, commits must always be present 
# 
bnf = ((show|select)("command") + delimitedList(users("team*") + team | users("user*")) + commits +  
            Optional(_from + timestamp('from') + _to + timestamp('to'))) 
 
test = 'show abc, def team, xyz commits from "Jan 10,2015" to "27/1/2015"' 
 
print bnf.parseString(test).dump() 

打印:

['show', 'abc', 'def', 'team', 'xyz', 'commits', 'from', 'Jan 10,2015', 'to', '27/1/2015'] 
- command: show 
- from: Jan 10,2015 
- team: ['def'] 
- to: 27/1/2015 
- user: ['abc', 'xyz']