i have the following string, need to turn it into a list without u'':
my_str = "[{u'name': u'squats', u'wrs': [[u'99', 8]], u'id': 2}]"
i can get rid of " by using
import ast str_w_quotes = ast.literal_eval(my_str)
then i do:
import json json.dumps(str_w_quotes)
and get
[{\"id\": 2, \"name\": \"squats\", \"wrs\": [[\"55\", 9]]}]
Is there a way to get rid of backslashes? the goal is:
[{"id": 2, "name": "squats", "wrs": [["55", 9]]}]
This works but doesn't seem too elegant
import json json.dumps(json.JSONDecoder().decode(str_w_quotes))
json.dumps thinks that the "
is part of a the string, not part of the json formatting.
import json json.dumps(json.load(str_w_quotes))
should give you:
[{"id": 2, "name": "squats", "wrs": [["55", 9]]}]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With