python - 使用 json.dump 创建包含 json 数组的有效 json 文件
问题描述
麦克维:
d1 = {"a":1}
d2 = {"b":2}
import json
with open('test2.json','w+') as file:
for d in [d1,d2]:
json.dump(d,file)
test2.json 内容:
{"a": 1}{"b": 2}
期望的结果:
[{"a": 1},{"b": 2}]
我怎样才能实现它?
解决方案
d1 = {"a":1}
d2 = {"b":2}
import json
array = [json.dumps(d) for d in [d1,d2]]
然后将数组写入文件
推荐阅读
- python - 在 azure 门户中看不到对我的 azure 应用程序清单的更改
- python - 将路径列表传递给另一个函数
- karate - 空手道加特林项目 - AbstractMethodError
- apache-spark - 如果有很多字段,如何让 pyspark 显示整个查询计划而不是...?
- java - LazyInitializationException : 为什么 Hibernate 不能在延迟加载时创建会话?
- terraform - terraform.tfvars 文件:为什么需要变量块
- git - Git分支策略问题
- linux - 显示监视器计时计算
- php - 如何在 Symfony 4 中正确配置 Gedmo Sluggable?
- kubernetes - 无法通过 Kubernetes 中的节点端口访问服务,用于 Docker 桌面窗口