暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Python中json.dump()和json.dumps()的区别

运维汪 2021-05-18
5670

一、图解

json.dumps(dict, indent):将Python对象转换成json字符串
json.dump(dict, file_pointer):将Python对象写入json文件

二、json.dumps()用法

1、用法

json.dumps(dict, indent):将Python对象转换成json字符串

参数:
dict:被转换的名称
indent:打印格式的参数

example:

    import json


    dictionary ={
    "id": "04",
    "name": "sunil",
    "depatment": "HR"
    }


    json_object = json.dumps(dictionary, indent=4)
    print(json_object)


    2、Python和Json数据类型的映射 

    Python
    JSON
    dictobject
    list, tuplearray
    str
    string
    int, float, int & float-derived Enumsnumber
    TRUETRUE
    FALSEFALSE
    Nonenull


    三、json.dumps()用法

    1、用法

    json.dump(dict, file_pointer):将Python对象写入json文件

    参数:
    dict:被转换的名称
    file_pointer:打开文件的指针

    example:

      import json


      dictionary = {
      "id": "04",
      "name": "sunil",
      "depatment": "HR"
      }


      with open("jsons_txt", "w") as filedata:
      json.dump(dictionary, filedata)


      四、写在最后

      李先生(Lemon),高级运维工程师(自称),SRE专家(目标),梦想在35岁买一辆保时捷。喜欢钻研底层技术,认为底层基础才是王道。一切新技术都离不开操作系统(CPU、内存、磁盘)、网络等。坚持输入输出,记录自己学习的点滴,在平凡中坚持前行,总有一天会遇见不一样的自己。公众号:运维汪(ID:Leeeee_Li)。


      文章转载自运维汪,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

      评论