1

一部のフレームをデコードするために pyparsing を使用していますが、デコードの出力に重複した結果が表示されており、その理由はまだわかりません。これが私が得ているものの1つの例です:

from pyparsing import *

def __Array__(s,l,t):
    n = int(t[0],16)
    buf << (n * asn1)

asn1 =      Forward()
buf =       Forward()

array =     Word(hexnums, exact=2).setParseAction(__Array__) + buf

asn1 <<     ( (Literal("01") + array).setResultsName("Array") \
            | (Literal("02") + Word(hexnums, exact=8)).setResultsName("Variable 1") \
            | (Literal("03") + Word(hexnums, exact=2)).setResultsName("Variable 2") )

structX =   Literal("C1").setResultsName("tag1") \
            + Literal("D1").setResultsName("tag2") \
            + Literal("E1").setResultsName("tag3") \
            + Literal("F1").setResultsName("tag4") \
            + asn1

structY =   Literal("C2").setResultsName("tag5") \
            + Literal("D2").setResultsName("tag6") \
            + Literal("F2").setResultsName("tag7") \
            + asn1

structZ =   Literal("C3").setResultsName("tag8") \
            + Literal("D3").setResultsName("tag9") \
            + asn1

header21 =  Literal("D1").setResultsName("header211") + structX \
            | Literal("D2").setResultsName("header212") + structY \
            | Literal("D3").setResultsName("header213") + structZ

header22 =  Literal("E1").setResultsName("header221") + structX \
            | Literal("E2").setResultsName("header222") + structY

header23 =  Literal("F1").setResultsName("header231") + structZ

header =    Literal("AA").setResultsName("header11") + header21 \
            | Literal("BB").setResultsName("header12") + header22 \
            | Literal("CC").setResultsName("header13") + header23

frame = "AA D1 C1 D1 E1 F1 01 02 02 00 00 00 00 03 00"
frame = frame.strip('').replace(' ','')

res = header.parseString(frame)
print(res.dump())

フレーム構造は次のとおりです。

AA          -- One of multiple tags : header11
    D1      -- One of multiple tags : header21
        C1      -- StructX - tag1
        D1      -- StructX - tag2
        E1      -- StructX - tag3
        F1      -- StructX - tag4  
                -- asn1 part --   
            01          -- Array
            02
                02          -- Variable 1
                    00 00 00 00 
                03          -- Variable 2
                    00

私が得る出力はこれです:

['AA', 'D1', 'C1', 'D1', 'E1', 'F1', '01', '02', '02', '00000000', '03', '00']
- Array: ['01', '02', '02', '00000000', '03', '00']
  - Variable 1: ['02', '00000000']
  - Variable 2: ['03', '00']
- Variable 1: ['02', '00000000']
- Variable 2: ['03', '00']
- header11: AA
- header211: D1
- tag1: C1
- tag2: D1
- tag3: E1
- tag4: F1

しかし、私が欲しいのはこれです:

['AA', 'D1', 'C1', 'D1', 'E1', 'F1', '01', '02', '02', '00000000', '03', '00']
- Array: ['01', '02', '02', '00000000', '03', '00']
  - Variable 1: ['02', '00000000']
  - Variable 2: ['03', '00']
- header11: AA
- header211: D1
- tag1: C1
- tag2: D1
- tag3: E1
- tag4: F1

ここで私が間違っていることを誰かが知っていますか?

4

1 に答える 1

1

変化する:

asn1 <<     ( (Literal("01") + array).setResultsName("Array") \
            | (Literal("02") + Word(hexnums, exact=8)).setResultsName("Variable 1") \
            | (Literal("03") + Word(hexnums, exact=2)).setResultsName("Variable 2") )

に:

asn1 <<     ( Group(Literal("01") + array).setResultsName("Array") \
            | (Literal("02") + Word(hexnums, exact=8)).setResultsName("Variable 1") \
            | (Literal("03") + Word(hexnums, exact=2)).setResultsName("Variable 2") )

Group は 2 つのことを行います。返された ParseResults 内にサブ構造を作成し、ネストされたパーサーで定義された結果名を分離します。

于 2012-06-25T15:29:59.387 に答える