# -*- coding: utf-8 -*-
import json
persons = [
{
"user": "老李",
"age": 22,
"weight":18.8
},
{
"user":"小要",
"age": 33,
"weight": 17.8
}
]
# 不写入文件直接读,json用dumps,ensure_ascii解决中文问题
json_str = json.dumps(persons, ensure_ascii=False)
print(json_str)
with open("person.json", "w", encoding="utf-8") as f:
# 写入文件用json时用dump
json.dump(persons, f, ensure_ascii=False)
#读取列表文件出来变成一行一行的字典
with open("person.json", "r", encoding="utf-8") as f:
persons = json.load(f)
print(type(persons))
for i in persons:
print(i)
python json_dump_load用法
最新推荐文章于 2024-06-28 10:51:29 发布