删除python列表中单引号的最佳方法是什么?
我有以下输入:
['{"name": "Brian", "city": "Seattle"}', '{"name": "Rick", "city": "Boston"}']
但我想要以下输出:
[{"name": "Brian", "city": "Seattle"}, {"name": "Rick", "city": "Boston"}]
答案 0 :(得分:0)
一种解决方案是使用json.loads
。
import json
a_list = ['{"name": "Brian", "city": "Seattle"}', '{"name": "Rick", "city": "Boston"}']
dict_list = [json.loads(x) for x in a_list]
输出:[{'name': 'Brian', 'city': 'Seattle'}, {'name': 'Rick', 'city': 'Boston'}]