2


この行を完全に解析する方法に頭を悩ませています。「(4801)」の部分で問題が発生しています。他のすべての要素は正常に取得されています。

# MAIN_PROG     ( 4801) Generated at 2010-01-25 06:55:00

これは私が今まで持っているものです

from pyparsing import nums, Word, Optional, Suppress, OneOrMore, Group, Combine, ParseException

unparsed_log_data = "# MAIN_PROG ( 4801) Generated at 2010-01-25 06:55:00.007    Type:  Periodic"

binary_name = "# MAIN_PROG"
pid = Literal("(" + nums + ")")
report_id = Combine(Suppress(binary_name) + pid)

year = Word(nums, max=4)
month = Word(nums, max=2)
day = Word(nums, max=2)
yearly_day = Combine(year + "-" + month + "-" + day)

clock24h = Combine(Word(nums, max=2) + ":" + Word(nums, max=2) + ":" + Word(nums, max=2) + Suppress("."))
timestamp = Combine(yearly_day + White(' ') + clock24h).setResultsName("timestamp")

time_bnf = report_id + Suppress("Generated at") + timestamp

time_bnf.searchString(unparsed_log_data)

編集: ポール、あなたが忍耐力を持っているなら、私はどのようにフィルタリングしますか

unparsed_log_data = 
"""  
# MAIN_PROG     ( 4801) Generated at 2010-01-25 06:55:00
bla bla bla   
multi line garbage  
bla bla  
Efficiency       |       38       38 100 |   3497061    3497081  99 |  
more garbage
"""

time_bnf = report_id + Suppress("Generated at") + timestamp  
partial_report_ignore = Suppress(SkipTo("Efficiency"))  

efficiency_bnf = Suppress("|") + integer.setResultsName("Efficiency") + Suppress(integer) + integer.setResultsName("EfficiencyPercent")

Efficiency_bnf.searchString(unparsed_log_data)とreport_and_effic.searchString(unparsed_log_data)はどちらも期待どおりにデータを返しますが、試してみると

report_and_effic = report_bnf + partial_report_ignore + Efficiency_bnf

report_and_effic.searchString(unparsed_log_data)は([]、{})を返します

EDIT2: コードを読み取る必要があります
。partial_report_ignore= Suppress(SkipTo( "Efficiency"、include = True))

4

1 に答える 1

2
pid = Literal("(" + nums + ")")

する必要があります

pid = "(" + Word(nums) + ")"

構文解析では、次のように「+」を使用して式オブジェクトに文字列を追加できます。

expr + "some string"

これは次のように解釈されます:

expr + Literal("some string")

あなたが書いLiteral("(" + nums + ")")た。numsは文字列「0123456789」で、のようにWordの作成の一部として使用されますWord(nums)。したがって、一致させようとしていたのは、「左括弧の後にnumsの後に右括弧が続く単語」ではなく、リテラル文字列「(0123456789)」を一致させようとしていました。

于 2011-03-01T02:35:13.587 に答える