pyparsing - 解析日期
pyparsing - parsing dates
我正在尝试从这种格式解析日期:
"11/2012-2014,2016,10/2012,11/2012-10/2014,2012-11/2012,".
预期结果是 (((11, 2012), (2014)), (2016), (10, 2012), ...)
错误的值:“11”
但是出了点问题。
month = Word(nums).setParseAction(self.validate_month)
year = Word(nums).setParseAction(self.validate_year)
date = Optional(month + Literal("/")) + year
date_range = Group(date + Optional(Literal("-") + date))
dates = date_range + ZeroOrMore(Literal(",") + date_range)
command = StringStart() + dates + StringEnd()
我哪里错了?
谢谢
要获得所需的输出,您需要做一些事情:
围绕您的日期表达式添加组
通过使用抑制而不是文字来抑制输出中的标点符号
查看下面的评论和示例输出:
# if there is a problem in your parse actions, you will need to post them
month = Word(nums)#.setParseAction(self.validate_month)
year = Word(nums)#.setParseAction(self.validate_year)
# wrap date in a Group
#date = Optional(month + Suppress("/")) + year
date = Group(Optional(month + Suppress("/")) + year)
date_range = Group(date + Optional(Suppress("-") + date))
dates = date_range + ZeroOrMore(Suppress(",") + date_range)
# your expression for dates can be replaced with this pyparsing helper
# dates = delimitedList(date_range)
# The trailing ',' causes an exception because of your use of StringEnd()
command = StringStart() + dates + StringEnd()
test = "11/2012-2014,2016,10/2012,11/2012-10/2014,2012-11/2012"
# you can also use parseAll=True in place of tacking StringEnd
# onto the end of your parser
command.parseString(test, parseAll=True).pprint()
版画
[[['11', '2012'], ['2014']],
[['2016']],
[['10', '2012']],
[['11', '2012'], ['10', '2014']],
[['2012'], ['11', '2012']]]
我正在尝试从这种格式解析日期:
"11/2012-2014,2016,10/2012,11/2012-10/2014,2012-11/2012,".
预期结果是 (((11, 2012), (2014)), (2016), (10, 2012), ...)
错误的值:“11”
但是出了点问题。
month = Word(nums).setParseAction(self.validate_month)
year = Word(nums).setParseAction(self.validate_year)
date = Optional(month + Literal("/")) + year
date_range = Group(date + Optional(Literal("-") + date))
dates = date_range + ZeroOrMore(Literal(",") + date_range)
command = StringStart() + dates + StringEnd()
我哪里错了?
谢谢
要获得所需的输出,您需要做一些事情:
围绕您的日期表达式添加组
通过使用抑制而不是文字来抑制输出中的标点符号
查看下面的评论和示例输出:
# if there is a problem in your parse actions, you will need to post them
month = Word(nums)#.setParseAction(self.validate_month)
year = Word(nums)#.setParseAction(self.validate_year)
# wrap date in a Group
#date = Optional(month + Suppress("/")) + year
date = Group(Optional(month + Suppress("/")) + year)
date_range = Group(date + Optional(Suppress("-") + date))
dates = date_range + ZeroOrMore(Suppress(",") + date_range)
# your expression for dates can be replaced with this pyparsing helper
# dates = delimitedList(date_range)
# The trailing ',' causes an exception because of your use of StringEnd()
command = StringStart() + dates + StringEnd()
test = "11/2012-2014,2016,10/2012,11/2012-10/2014,2012-11/2012"
# you can also use parseAll=True in place of tacking StringEnd
# onto the end of your parser
command.parseString(test, parseAll=True).pprint()
版画
[[['11', '2012'], ['2014']],
[['2016']],
[['10', '2012']],
[['11', '2012'], ['10', '2014']],
[['2012'], ['11', '2012']]]