首页 > 解决方案 > Python:在 json.dumps() 中选择特定列

问题描述

我需要使用 json.dumps() 从 python 字典中选择特定的列。

例如。

dict={"Greet":"Hello","Bike":Yamaha","Car":"Jaguar"}
r=json.dumps(Only want "Bike":Yamaha","Car":"Jaguar")

注意:不能将其存储到其他字典中并使用它。因为我也想在我的代码中使用 First K,V 对。

标签: pythonpython-jsons

解决方案


final = list(mydict.items())[1:] #extra key:value tuple list slice of the portion you need
r=json.dumps(dict(final)) #cast to dictionary and dump

输出

{"Bike": "Yamaha", "Car": "Jaguar"}

推荐阅读