python structuring of dict with nested

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jhon123
    New Member
    • Aug 2018
    • 2

    python structuring of dict with nested

    I have nested list below as test_data which is my input also and expected_data is the formatted output my question now is how to get expected output from test_data.

    ###Input given
    Code:
     
     test_data = [[u'', u'Sr. No.', u'TestCase ID', u'Iteration', u'Pre-requisites', u'Action:Parameter:Verification'], 
    			  [u'', 1.0, u'IAE_001', 1.0, u"SET_MOBILE_NETWORK:DUT1:DUT1,network='TBD'\nSET_MOBILE_NETWORK:DUT2:DUT2,network='TBD'",'SEND_SMS:DUT1,DUT2,Msg:DUT1,Msg="Test Message",MsgState=\'Delivered\'\nRECEIVE_SMS:DUT2,DUT1,Msg:DUT2,Msg="Test Message",MsgState=\'Read\''],
                  
    			  [u'', 2.0, u'IAE_002', 1.0, u"SET_MOBILE_NETWORK:DUT1:DUT1,network='TBD'\nSET_MOBILE_NETWORK:DUT2:DUT2,network='TBD'\nSET_MOBILE_DATA_ON:DUT1\nSET_MOBILE_DATA_ON:DUT2", u'SEND_SMS:DUT1,DUT2,Msg:DUT1,Msg="Test Message",MsgState=\'Delivered\'\nRECEIVE_SMS:DUT2,DUT1,Msg:DUT2,Msg="Test Message",MsgState=\'Read\'\nSET_MOBILE_DATA_OFF:DUT1\nSET_MOBILE_DATA_OFF:DUT2']]
    ###Expected Output --

    Code:
    expected_data = {
        'AMBS_001':{
            'Iteration': 1,
            'Pre-requisites': ["ADV_MSG_ENABLE:DUT1:DUT1,mode='enabled'",
                              "ADV_MSG_ENABLE:DUT2:DUT1,mode='enabled'",
                              "WEB_APP_LOGIN:DUT1"],
            'Action:Parameter:Verification':["SEND_SMS:DUT1,DUT2,Msg:DUT1,Msg=\"Test Message\",MsgState='Delivered',MsgMode='Pager'",
                     "READ_SMS:DUT1,DUT2,Msg:DUT2,Msg=\"Test Message\",MsgState='Read',MsgMode='Pager'"]
        },
        'AMBS_002':{
            'Iteration': 2,
            'Pre-requisites':["ADV_MSG_ENABLE:DUT1:DUT1,mode='enabled'",
                              "ADV_MSG_ENABLE:DUT2:DUT1,mode='enabled'",
                              "WEB_APP_LOGIN:DUT1",
                              "WEB_APP_LOGIN:DUT2"],
            'Action:Parameter:Verification':["AIRPLANE_MODE_ON:DUT1:DUT1,mode='enabled' (AIRPLANE_MODE:DUT1,ON:DUT1,MODE=(ON))",
                      "SEND_SMS:DUT1,DUT2,Msg:DUT1,Msg=\"Test Message\",MsgState='Delivered',MsgMode='Pager'",
                      "AIRPLANE_MODE_OFF:DUT1:DUT1,mode='disabled'",
                      "WEB_APP_READ_SMS:DUT2,DUT1,Msg:DUT2,Msg=\"Test Message\",MsgState='Read',MsgMode='Pager'"]
        }
    }
    ##this is what I have tried
    Code:
    heading=test_data.pop(0)
    print heading
    [u'', u'Sr. No.', u'TestCase ID', u'Iteration', u'Pre-requisites', u'Action:Parameter:Verification']
    test_data1=test_data.pop(0)
    print test_data1
    [u'', 1.0, u'IAE_001', 1.0, u"SET_MOBILE_NETWORK:DUT1:DUT1,network='TBD'\nSET_MOBILE_NETWORK:DUT2:DUT2,network='TBD'", 'SEND_SMS:DUT1,DUT2,Msg:DUT1,Msg="Test Message",MsgState=\'Delivered\'\nRECEIVE_SMS:DUT2,DUT1,Msg:DUT2,Msg="Test Message",MsgState=\'Read\'']
    test_data2=test_data.pop(0)
    print test
    
    Traceback (most recent call last):
      File "<pyshell#56>", line 1, in <module>
        print test
    NameError: name 'test' is not defined
    _
    print test_data2
    [u'', 2.0, u'IAE_002', 1.0, u"SET_MOBILE_NETWORK:DUT1:DUT1,network='TBD'\nSET_MOBILE_NETWORK:DUT2:DUT2,network='TBD'\nSET_MOBILE_DATA_ON:DUT1\nSET_MOBILE_DATA_ON:DUT2", u'SEND_SMS:DUT1,DUT2,Msg:DUT1,Msg="Test Message",MsgState=\'Delivered\'\nRECEIVE_SMS:DUT2,DUT1,Msg:DUT2,Msg="Test Message",MsgState=\'Read\'\nSET_MOBILE_DATA_OFF:DUT1\nSET_MOBILE_DATA_OFF:DUT2']
    I have no clue how to restructure it in nested dictionary form as shown for expected_data
    Last edited by jhon123; Aug 17 '18, 06:36 AM. Reason: for more clarity
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    jhon123:
    Welcome to Bytes.com!

    You give us the problem; however, you have not shown your work, you have not told us what you are actually getting, nor explained what you have tried to solve the problem on your own.

    If you will show your work, someone might be able to point you in the right direction.

    Please remember to format code/script using the [CODE/] tool in the formatting bar - and take a tour through our FAQ section for some suggestions on how to post a question that will get answers.

    Comment

    • jhon123
      New Member
      • Aug 2018
      • 2

      #3
      thanks for your suggestion zmbd. have updated the same

      Comment

      Working...