zoukankan      html  css  js  c++  java
  • Python学习笔记(三)——dump、dumps、load、loads的区别

    这四个是比C系语言更好用的表现,在处理数据方面,Python发挥了其易于编程的优势,废话不多说,之间在代码中来表现区别:

    import json
    Dict = {"name":"Wayne", 123:123, abc:{123:'123'}}#这里可以看出来,key和value的类型都不是说要一致,跟JS的属性很像
    #dump 将输入转换成str,写入文件
    with open("./file.json", "w") as f:
        json.dump(Dict, f)
    #load 从json文件读取数据
    with open("./file.json", "r") as f:
        Dict = json.load(f)
        print(Dict)
    #dumps 这里的s就是string之意,转换成str并返回
    Str = json.dumps(Dict)
    with open("./file.json", "w") as f:
        f.write(Str)
    #loads 将str转换成dict类型
    Dict = json.loads(Str)

    在处理json数据方面,使用频繁。

  • 相关阅读:
    js(四) 全选/全不选和反选
    js(三) ajax异步局部刷新技术底层代码实现
    js(二) 实现省市联动(json)
    接口
    内部类
    封装
    Static关键字
    this关键字
    带参数的方法
    abstract关键字
  • 原文地址:https://www.cnblogs.com/wayne-tao/p/12683839.html
Copyright © 2011-2022 走看看