468,291 Members | 1,655 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,291 developers. It's quick & easy.

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
Expand|Select|Wrap|Line Numbers
  1.  
  2.  test_data = [[u'', u'Sr. No.', u'TestCase ID', u'Iteration', u'Pre-requisites', u'Action:Parameter:Verification'], 
  3.               [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\''],
  4.  
  5.               [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 --

Expand|Select|Wrap|Line Numbers
  1. expected_data = {
  2.     'AMBS_001':{
  3.         'Iteration': 1,
  4.         'Pre-requisites': ["ADV_MSG_ENABLE:DUT1:DUT1,mode='enabled'",
  5.                           "ADV_MSG_ENABLE:DUT2:DUT1,mode='enabled'",
  6.                           "WEB_APP_LOGIN:DUT1"],
  7.         'Action:Parameter:Verification':["SEND_SMS:DUT1,DUT2,Msg:DUT1,Msg=\"Test Message\",MsgState='Delivered',MsgMode='Pager'",
  8.                  "READ_SMS:DUT1,DUT2,Msg:DUT2,Msg=\"Test Message\",MsgState='Read',MsgMode='Pager'"]
  9.     },
  10.     'AMBS_002':{
  11.         'Iteration': 2,
  12.         'Pre-requisites':["ADV_MSG_ENABLE:DUT1:DUT1,mode='enabled'",
  13.                           "ADV_MSG_ENABLE:DUT2:DUT1,mode='enabled'",
  14.                           "WEB_APP_LOGIN:DUT1",
  15.                           "WEB_APP_LOGIN:DUT2"],
  16.         'Action:Parameter:Verification':["AIRPLANE_MODE_ON:DUT1:DUT1,mode='enabled' (AIRPLANE_MODE:DUT1,ON:DUT1,MODE=(ON))",
  17.                   "SEND_SMS:DUT1,DUT2,Msg:DUT1,Msg=\"Test Message\",MsgState='Delivered',MsgMode='Pager'",
  18.                   "AIRPLANE_MODE_OFF:DUT1:DUT1,mode='disabled'",
  19.                   "WEB_APP_READ_SMS:DUT2,DUT1,Msg:DUT2,Msg=\"Test Message\",MsgState='Read',MsgMode='Pager'"]
  20.     }
  21. }
##this is what I have tried
Expand|Select|Wrap|Line Numbers
  1. heading=test_data.pop(0)
  2. print heading
  3. [u'', u'Sr. No.', u'TestCase ID', u'Iteration', u'Pre-requisites', u'Action:Parameter:Verification']
  4. test_data1=test_data.pop(0)
  5. print test_data1
  6. [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\'']
  7. test_data2=test_data.pop(0)
  8. print test
  9.  
  10. Traceback (most recent call last):
  11.   File "<pyshell#56>", line 1, in <module>
  12.     print test
  13. NameError: name 'test' is not defined
  14. _
  15. print test_data2
  16. [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']
  17.  
I have no clue how to restructure it in nested dictionary form as shown for expected_data
Aug 16 '18 #1
2 1129
zmbd
5,400 Expert Mod 4TB
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.
Aug 16 '18 #2
thanks for your suggestion zmbd. have updated the same
Aug 17 '18 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by kk | last post: by
15 posts views Thread by Cruella DeVille | last post: by
22 posts views Thread by sapsi | last post: by
1 post views Thread by jholg | last post: by
12 posts views Thread by brad | last post: by
20 posts views Thread by Mr.SpOOn | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.