python - 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 对。
解决方案
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"}
推荐阅读
- kubernetes - 为什么 Istio 要求 Pod 至少属于 Kubernetes 中的一个 Service?
- java - 将 Java Gradle 多项目推送到 Git
- python - Msys2 无法使用 python + qt5 解决依赖关系
- html - 如何使用引导程序在移动设备上创建响应式 HTML?
- sql-server - 使用条件列表 SQL Server (UPDLOCK, READPAST, ROWLOCK)
- apache-spark - 在远程配置单元上运行 sql 查询时出现未知主机错误
- swiftui - macCatalyst/ SwiftUI 触控栏
- c# - 从 .Net Core 2.0 调用 SOAP api 时出错
- build - Azure Dev Ops:构建过期时重新启动自动构建策略
- jquery - 计算多行jquery动态表单