pyparsing:在日期之间对文本进行分组
我的日志文件包含日期/时间,在下一个日期/时间之间有不同的行 前 时间日期pyparsing:在日期之间对文本进行分组,pyparsing,Pyparsing,我的日志文件包含日期/时间,在下一个日期/时间之间有不同的行 前 时间日期 2018年7月2日13:55:00.983 msecVal = pyparsing.Word(pyparsing.nums, max=3) numPair = pyparsing.Word(pyparsing.nums, exact=2) dateStr = pyparsing.Combine(numPair + '/' + numPair + '/' + numPair) timeString = pyparsing
2018年7月2日13:55:00.983
msecVal = pyparsing.Word(pyparsing.nums, max=3)
numPair = pyparsing.Word(pyparsing.nums, exact=2)
dateStr = pyparsing.Combine(numPair + '/' + numPair + '/' + numPair)
timeString = pyparsing.Combine(numPair + ':' + numPair + ':' + numPair\
+ '.' + msecVal)
日志文件将被删除
time:date: line of text
possible 2nd line of text
possible 3rd line of text...
time:date: line of text
time:date: line of text
possible 2nd line of text
possible 3rd line of text...
possible <n> line of text...
time:date: line of text
如果每个时间/日期条目都是一行,我可以这样做。。它跨越了随机的多行,直到下一个我遇到问题的时间/日期条目。以下是我对日志条目定义的解释: 一行开头的日期时间,后跟冒号,后跟所有内容 直到行开头的下一个日期时间,即使可能有日期时间 嵌入线路中。” 要解决此问题,您需要两个pyparsing功能:
- LineStart-区分行开始处的日期时间与行正文中的日期时间
- SkipTo-快速跳过非结构化文本,直到找到匹配的表达式
sample = """\
2/07/18 13:55:00.983: line of text
possible 2nd line of text
possible 3rd line of text...
2/07/19 13:55:00.983: line of text
2/07/20 13:55:00.983: line of text
possible 2nd line of text
possible 3rd line of text...
possible <n> line of text...
2/07/21 13:55:00.983: line of text
"""
print(pp.OneOrMore(logEntry).parseString(sample).dump())
否则它将与样本日期中的前导个位数“2”不匹配
dateTime = dateStr + timeString
# log entry date-time keys only match if they are at the start of the line
dateTimeKey = pp.LineStart() + dateTime
# define a log entry as a date-time key, followed by everything up to the next
# date-time key, or to the end of the input string
# (use results names to make it easy to get at the parts of the log entry)
logEntry = pp.Group(dateTimeKey("time") + ':' + pp.Empty()
+ pp.SkipTo(dateTimeKey | pp.StringEnd())("body"))
sample = """\
2/07/18 13:55:00.983: line of text
possible 2nd line of text
possible 3rd line of text...
2/07/19 13:55:00.983: line of text
2/07/20 13:55:00.983: line of text
possible 2nd line of text
possible 3rd line of text...
possible <n> line of text...
2/07/21 13:55:00.983: line of text
"""
print(pp.OneOrMore(logEntry).parseString(sample).dump())
[['2/07/18', '13:55:00.983', ':', 'line of text\n possible 2nd line of text\n possible 3rd line of text...\n 2/07/19 13:55:00.983: line of text'], ['2/07/20', '13:55:00.983', ':', 'line of text\n possible 2nd line of text\n possible 3rd line of text...\n possible <n> line of text...'], ['2/07/21', '13:55:00.983', ':', 'line of text']]
[0]:
['2/07/18', '13:55:00.983', ':', 'line of text\n possible 2nd line of text\n possible 3rd line of text...\n 2/07/19 13:55:00.983: line of text']
- body: 'line of text\n possible 2nd line of text\n possible 3rd line of text...\n 2/07/19 13:55:00.983: line of text'
- time: ['2/07/18', '13:55:00.983']
[1]:
['2/07/20', '13:55:00.983', ':', 'line of text\n possible 2nd line of text\n possible 3rd line of text...\n possible <n> line of text...']
- body: 'line of text\n possible 2nd line of text\n possible 3rd line of text...\n possible <n> line of text...'
- time: ['2/07/20', '13:55:00.983']
[2]:
['2/07/21', '13:55:00.983', ':', 'line of text']
- body: 'line of text'
- time: ['2/07/21', '13:55:00.983']
numPair = pp.Word(pp.nums, max=2)