Reputation: 2873
How do I transform a dictionary input to a real dictionary that I can process?
When I execute an external command, this is what I get.
{'aaa': {'test_a': 0.11666666666667,
'test_b': 1,
'total_c': 0.11666666666667},
'bbb': {'test_a': 32.883333333332999,
'test_b': 1,
'total_c': 0.11666666666667},
'ccc': {'test_a': 11, 'test_b': 31, 'test'_c': 33}}
So, as you can see, the above is already a dictionary-format already. I was just thinking of doing something like.
#!/usr/bin/python
import command
result = commands.getoutput('<execute_external_command')
So that 'result' becomes a dictionary and I can just process it like any dictionary.
Upvotes: 0
Views: 185
Reputation: 20288
from simplejson import loads
result='{"name":"Anton"}'
dictionary=loads(result)
print dictionary
result="{'name':'Anton'}"
dictionary=loads(result.replace("'",'"'))
print dictionary
Upvotes: 4
Reputation: 798606
>>> ast.literal_eval("""{'aaa': {'test_a': 0.11666666666667,
... 'test_b': 1,
... 'total_c': 0.11666666666667},
... 'bbb': {'test_a': 32.883333333332999,
... 'test_b': 1,
... 'total_c': 0.11666666666667},
... 'ccc': {'test_a': 11, 'test_b': 31, 'test_c': 33}}""")
{'aaa': {'total_c': 0.11666666666667, 'test_b': 1, 'test_a': 0.11666666666667}, 'bbb': {'total_c': 0.11666666666667, 'test_b': 1, 'test_a': 32.883333333333}, 'ccc': {'test_c': 33, 'test_b': 31, 'test_a': 11}}
Upvotes: 1