python 学习积累

持续更新中
感受python的强大之case列举:

1. 生成的map list要经过json格式化写入文件,请用python实现这一需求

import json
map={"name": "张三", "age": 18, "address": "北京"}
list=[]
for i in range(3):
  list.append(map)

with open('output.json','w', encoding='utf-8') as fp:
  json.dump(list, fp, ensure_ascii=False, indent=4)

生成文件如下

[
    {
        "name": "张三",
        "age": 18,
        "address": "北京"
    },
    {
        "name": "张三",
        "age": 18,
        "address": "北京"
    },
    {
        "name": "张三",
        "age": 18,
        "address": "北京"
    }
]

json.dump不带indent=4 则生成一行内容:

[{"name": "张三", "age": 18, "address": "北京"}, {"name": "张三", "age": 18, "address": "北京"}, {"name": "张三", "age": 18, "address": "北京"}]

你可能感兴趣的:(python,学习)