2

I have a string that contains my data like this:

result = "[
           {'service': {'option1': 'abc', 'others': 'blah blah'}},
           {'service': {'option2': 'None', 'others': 'None'}}
          ]\n
          [
           {'rules': {'permissions': 'allow', 'opp': 'evening', 'order': '10', 'self': 'me'}},
            {'rules': {'permissions': 'not allowed', 'opp': 'None\\n', 'order': 'None', 'self': 'None'}}
          ]"

I want to convert it into a proper list like:

result = [[
          {'service': {'option1': 'abc', 'others': 'blah blah'}},
          {'service': {'option2': 'None', 'others': 'None'}}
         ],
         [
          {'rules': {'permissions': 'allow', 'opp': 'evening', 'order': '10', 'self': 'me'}},
          {'rules': {'permissions': 'not allowed', 'opp': 'None\\n', 'order': 'None', 'self': 'None'}}
         ]]

Kindly help me out! I have tried .split(), .join() but unable to get the exact format like above.

1 Answer 1

4

you can use ast, ( Abstract Syntax Trees ) with a minor modification of the string,

import ast
ast.literal_eval( '[{}]'.format( result.replace( ']\n', '],' ) ) )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.