json中的dump和dumps的区别

json

  • json 是一种标准的数据交换格式,独立于编程语言和计算平台。它其实也是一种字符串,默认编码是Unicode。
  • 字典和json 的互转。
import json

a={"name":"老王"}
print(type(a))
# 字典转化为json
b_json =json.dumps(a,ensure_ascii=False)
print(type(b_json))
print(b_json)
# 转化为字典
c_dict=json.loads(b_json)
print(c_dict)
print(type(c_dict))
  • dump 和 load
a={"name":"路遥","book":"平凡的世界"}
# 把Python的类型写入到类文件对象中
with open("file.txt","w",encoding="utf-8") as f:
    json.dump(a,f,ensure_ascii=False,indent=2)# fp参数指的是类文件对象,具有rend()或write()方法的对象。
    #把类文件对象转化为python 的类型
with open("file.txt","r") as f:
  	json.load(f)

猜你喜欢

转载自blog.csdn.net/weixin_44224529/article/details/88901711