我正在使用PyParsing和以下代码来解析日期字符串:
from pyparsing import *
# Day details (day number, superscript and day name)
daynum = Word(nums, max=2)
superscript = oneOf("th rd st nd", caseless=True)
day = oneOf("Mon Monday Tue Tues Tuesday Wed Weds Wednesday Thu Thur Thurs Thursday Fri Friday Sat Saturday Sun Sunday", caseless=True)
full_day_string = Optional(day).suppress() & daynum + Optional(superscript).suppress()
# Month names, with abbreviations
month = oneOf("Jan January Feb February Mar March Apr April May Jun June Jul July Aug August Sep September Oct October Nov November Dec December", caseless=True)
# Year
year = Word(nums, exact=4)
# Full string
date = Each( [full_day_string("day"), Optional(month)("month"), Optional(year)("year")])
当我运行解析器并转储生成的结构时,我得到以下内容:
In [2]: r = date.parseString("23rd Jan 2012")
In [3]: print r.dump()
['23', 'Jan', '2012']
- day: ['23']
Every子句中的可选字段似乎已被正确拾取(您可以在顶部的列表中看到),但它们没有像它们那样被标记。
如果我将Each
更改为And
,则可以正常工作。
答案 0 :(得分:4)
作为解决方法,请更改:
date = Each( [full_day_string("day"), Optional(month)("month"), Optional(year)("year")])
到
date = Each( [full_day_string("day"), Optional(month("month")), Optional(year("year"))])
但这只是告诉我pyparsing有一个错误。我看了一下,还没找到它。我跑下去的时候会发表新的评论。