James Xu
James Xu

Reputation: 155

convert a string of list into a list

In python3, Can you please help convert the dictionary values that is a long string into a list:

d1 = 
{'result': '[{"pod_name": "kafka-9", "resolution_ms": 60000, "value": 420.85}, 
{"pod_name": "kafka-3", "resolution_ms": 60000, "value": 418.0}]'...}

The dict values is a string, not a list as it should be , how can I conver the dictionary into a list like

res = 
{'result': [{"pod_name": "kafka-9", "resolution_ms": 60000, "value": 420.85}, 
{"pod_name": "kafka-3", "resolution_ms": 60000, "value": 418.0}]...}

Upvotes: 1

Views: 75

Answers (1)

Sachin Kohli
Sachin Kohli

Reputation: 1986

Try this; using json

import json
df1 = {'result': '[{"pod_name": "kafka-9", "resolution_ms": 60000, "value": 420.85},{"pod_name": "kafka-3", "resolution_ms": 60000, "value": 418.0}]'}
result = [json.loads(idx.replace("'", '"')) for idx in [df1['result']]]
df1['result'] = result[0]

# Output of df1;
{'result': [{'pod_name': 'kafka-9', 'resolution_ms': 60000, 'value': 420.85},
            {'pod_name': 'kafka-3', 'resolution_ms': 60000, 'value': 418.0}]}

Upvotes: 1

Related Questions