By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,501 Members | 2,817 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,501 IT Pros & Developers. It's quick & easy.

python structuring of dict with nested

P: 2
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
  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\''],
  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
  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']
I have no clue how to restructure it in nested dictionary form as shown for expected_data
Aug 16 '18 #1
Share this Question
Share on Google+
2 Replies

Expert Mod 5K+
P: 5,397
Welcome to!

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

P: 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.