How to analyze the following text using pyparsing

2014-01-19
2014-01-19
  • ningth ningth
    ningth ningth
    2014-01-19

    How to analyze the following text, please help me
    -----------begin-----
    d string : "Function"
    e string : "qq,Sub=1,ManagedElement=1101,Function=0831||(w)L"
    jj string : "7000||N"
    b IRPTime :2014-01-18 16:44:42
    c string : "Sub=1,Node=1,IRPAgent=1,AlarmIRP=1"
    g short : 22
    h short : 2
    remainder_of_body struct {
    name_value_pairs sequence {
    name_value_pairs_0 struct {
    name string : "a"
    value any : {
    long : 144
    }
    }
    name_value_pairs_1 struct {
    name string : "i"
    value any : {
    string : "7000||N"
    }
    }
    name_value_pairs_2 struct {
    name string : "w"
    value any : {
    sequence {
    _0 struct {
    source string : ""
    notif_id_set sequence {
    }
    }
    }
    }
    }
    name_value_pairs_3 struct {
    name string : "p"
    value any : {
    boolean : 0
    }
    }
    name_value_pairs_4 struct {
    name string : "q"
    value any : {
    string : ""
    }
    }
    name_value_pairs_5 struct {
    name string : "s"
    value any : {
    string : ""
    }
    }
    name_value_pairs_6 struct {
    name string : "r"
    value any : {
    struct {
    attribute_id string : ""
    observed_value float : 0
    threshold_level union {
    value boolean : 1
    value struct {
    indication enum : UP
    low union {
    value boolean : 1
    value float : 0
    }
    high float : 0
    }
    }
    arm_time string : ""
    }
    }
    }
    name_value_pairs_7 struct {
    name string : "t"
    value any : {
    sequence {
    _0 struct {
    attribute_name string : "Sn"
    old_value any : {
    string : ""
    }
    new_value any : {
    string : ""
    }
    }
    }
    }
    }
    name_value_pairs_8 struct {
    name string : "u"
    value any : {
    sequence {
    _0 struct {
    attribute_name string : "Attributes"
    value any : {
    string : ""
    }
    }
    }
    }
    }
    name_value_pairs_9 struct {
    name string : "v"
    value any : {
    string : "||"
    }
    }
    name_value_pairs_10 struct {
    name string : "j"
    value any : {
    string : ""
    }
    }
    name_value_pairs_11 struct {
    name string : "ps"
    value any : {
    string : "CRITICAL"
    }
    }
    name_value_pairs_12 struct {
    name string : "at"
    value any : {
    string : "communicationsAlarm"
    }
    }
    name_value_pairs_13 struct {
    name string : "f"
    value any : {
    string : "375"
    }
    }
    }
    remainder_of_non_filterable_body any : {
    }
    }
    -----------end------

     
    • Paul McGuire
      Paul McGuire
      2014-01-19

      This parser will handle all but named_value_6. I leave the definitions of
      enum and union up to you.

      -- Paul

      from pyparsing import *

      from datetime import datetime

      identifier = Word(alphas+'', alphanums+'')

      integer = Regex(r'[+-]?\d+').setParseAction(lambda t: int(t[0]))

      real = Regex(r'[+-]?\d+.\d*([Ee][+-]?\d+)?').setParseAction(lambda t:
      float(t[0]))

      timestamp = Regex(r'\d\d\d\d-\d\d-\d\d
      \d\d:\d\d:\d\d').setParseAction(lambda t: datetime.strptime(t[0], "%Y-%m-%d
      %H:%M:%S"))

      string_ = dblQuotedString.copy().setParseAction(removeQuotes)

      BOOLEAN,LONG,SHORT,FLOAT,IRPTIME,STRING,ANY,SEQUENCE,STRUCT,UNION,ENUM =
      map(lambda s: Keyword(s).suppress(),

      "boolean long short float IRPTime string any sequence struct union
      

      enum".split())

      LBRACE,RBRACE,COLON = map(Suppress,"{}:")

      boolean_value = BOOLEAN + COLON + integer

      long_value = LONG + COLON + integer

      short_value = SHORT + COLON + integer

      float_value = FLOAT + COLON + (real | integer)

      string_value = STRING + COLON + string_

      time_value = IRPTIME + COLON + timestamp

      struct_contents = Forward()

      sequence_contents = Forward()

      any_contents = Forward()

      struct_value = STRUCT - struct_contents

      sequence_value = SEQUENCE - sequence_contents

      any_value = ANY + COLON - any_contents

      type_value = boolean_value | long_value | short_value | float_value |
      string_value | time_value | struct_value | sequence_value | any_value

      name_value = Group(identifier + type_value)

      struct_contents << Group(LBRACE + name_value*(0,None) + RBRACE)

      sequence_contents << Group(LBRACE + (identifier + type_value)*(0,None) +
      RBRACE)

      any_contents << Group(LBRACE + type_value*(0,None) + RBRACE)

      results = (name_value*(1,None)).parseString(sample)

      import pprint

      pprint.pprint(results.asList())

      From: ningth ningth [mailto:ningth@users.sf.net]
      Sent: Saturday, January 18, 2014 11:41 PM
      To: [pyparsing:discussion]
      Subject: [pyparsing:discussion] How to analyze the following text using
      pyparsing

      How to analyze the following text, please help me
      -----------begin-----
      d string : "Function"
      e string : "qq,Sub=1,ManagedElement=1101,Function=0831||(w)L"
      jj string : "7000||N"
      b IRPTime :2014-01-18 16:44:42
      c string : "Sub=1,Node=1,IRPAgent=1,AlarmIRP=1"
      g short : 22
      h short : 2
      remainder_of_body struct {
      name_value_pairs sequence {
      name_value_pairs_0 struct {
      name string : "a"
      value any : {
      long : 144
      }
      }
      name_value_pairs_1 struct {
      name string : "i"
      value any : {
      string : "7000||N"
      }
      }
      name_value_pairs_2 struct {
      name string : "w"
      value any : {
      sequence {
      _0 struct {
      source string : ""
      notif_id_set sequence {
      }
      }
      }
      }
      }
      name_value_pairs_3 struct {
      name string : "p"
      value any : {
      boolean : 0
      }
      }
      name_value_pairs_4 struct {
      name string : "q"
      value any : {
      string : ""
      }
      }
      name_value_pairs_5 struct {
      name string : "s"
      value any : {
      string : ""
      }
      }
      name_value_pairs_6 struct {
      name string : "r"
      value any : {
      struct {
      attribute_id string : ""
      observed_value float : 0
      threshold_level union {
      value boolean : 1
      value struct {
      indication enum : UP
      low union {
      value boolean : 1
      value float : 0
      }
      high float : 0
      }
      }
      arm_time string : ""
      }
      }
      }
      name_value_pairs_7 struct {
      name string : "t"
      value any : {
      sequence {
      _0 struct {
      attribute_name string : "Sn"
      old_value any : {
      string : ""
      }
      new_value any : {
      string : ""
      }
      }
      }
      }
      }
      name_value_pairs_8 struct {
      name string : "u"
      value any : {
      sequence {
      _0 struct {
      attribute_name string : "Attributes"
      value any : {
      string : ""
      }
      }
      }
      }
      }
      name_value_pairs_9 struct {
      name string : "v"
      value any : {
      string : "||"
      }
      }
      name_value_pairs_10 struct {
      name string : "j"
      value any : {
      string : ""
      }
      }
      name_value_pairs_11 struct {
      name string : "ps"
      value any : {
      string : "CRITICAL"
      }
      }
      name_value_pairs_12 struct {
      name string : "at"
      value any : {
      string : "communicationsAlarm"
      }
      }
      name_value_pairs_13 struct {
      name string : "f"
      value any : {
      string : "375"
      }
      }
      }
      remainder_of_non_filterable_body any : {
      }
      }
      -----------end------


      How to analyze the following text using pyparsing
      https://sourceforge.net/p/pyparsing/discussion/337293/thread/8c797f45/?limi t=50#3dc1


      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/pyparsing/discussion/337293/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/


      This email is free from viruses and malware because avast! Antivirus protection is active.
      http://www.avast.com

       
      Attachments