如何在 Python 中将 [{'id':1'}] 之类的字符串转换为列表 [{'id':1'}]

其实我有这样的输出

[{'images': 'static/files/7_8731.jpg', 'p_weight': '4kg', 'p_id': '8731', 'p_name': 'Chicken', 'c_name': 'Eggs, Meat & Fish', 'p_cost': '1000', 'c_id': '7'}, {'images': 'static/files/6_3124.jpg', 'p_weight': '3pcs', 'p_id': '3124', 'p_name': 'Mobs', 'c_name': 'Cleaning & Houshold', 'p_cost': '1500', 'c_id': '6'}, {'images': 'static/files/1_8460.jpg', 'p_weight': '4kg', 'p_id': '8460', 'p_name': 'Healthy Fruit Mixer', 'c_name': 'Fruits & Vegetables', 'p_cost': '1000', 'c_id': '1'}, {'images': 'static/files/5_7110.jpg', 'p_weight': '1kg', 'p_id': '7110', 'p_name': 'French fries', 'c_name': 'Snacks', 'p_cost': '500', 'c_id': '5'}]

此数据的类型是字符串,我想将此数据更改为在 python 中具有相同结构的列表..请任何人都可以帮助我..


胡说叔叔
浏览 245回答 1
1回答

DIEA

Json 包只接受双引号,所以我们应该首先将字符串转换为 have"而不是'然后load数据import jsonlist_str = "[{'images': 'static/files/7_8731.jpg', 'p_weight': '4kg', 'p_id': '8731', 'p_name': 'Chicken', 'c_name': 'Eggs, Meat & Fish', 'p_cost': '1000', 'c_id': '7'}, {'images': 'static/files/6_3124.jpg', 'p_weight': '3pcs', 'p_id': '3124', 'p_name': 'Mobs', 'c_name': 'Cleaning & Houshold', 'p_cost': '1500', 'c_id': '6'}, {'images': 'static/files/1_8460.jpg', 'p_weight': '4kg', 'p_id': '8460', 'p_name': 'Healthy Fruit Mixer', 'c_name': 'Fruits & Vegetables', 'p_cost': '1000', 'c_id': '1'}, {'images': 'static/files/5_7110.jpg', 'p_weight': '1kg', 'p_id': '7110', 'p_name': 'French fries', 'c_name': 'Snacks', 'p_cost': '500', 'c_id': '5'}]"list_python = json.loads(list_str.replace('\'', '"'))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python