0

I have a string ouput which looks like below

result = "[(u'Delhi', 20199330), (u'Mumbai', 134869470), (u'Kolkata', 6678446)]"

Now I want to convert it to a regular array. So this is what I do

import json

print(json.loads(result))

But I get the following error

ValueError: No JSON object could be decoded

Now I know result is not a json. But python does convert a string list into a regular list by doing something like

some_list = "[10, 20, 30]"

print(json.loads(some_list))

So I was hoping it would convert my result which is a string list of tuples into a regular list of tuples. But it throws error.

How can I convert result into a regular list?

Souvik Ray
  • 2,899
  • 5
  • 38
  • 70

1 Answers1

1

Try this,

>>> result = "[(u'Delhi', 20199330), (u'Mumbai', 134869470), (u'Kolkata', 6678446)]"
>>> import ast
>>> ast.literal_eval(result)
[('Delhi', 20199330), ('Mumbai', 134869470), ('Kolkata', 6678446)]
shaik moeed
  • 5,300
  • 1
  • 18
  • 54