zoukankan      html  css  js  c++  java
  • python 处理 json 四个函数dumps、loads、dump、load的区别

    1 .json.dumps() 函数是将一个 Python 数据类型列表(可以理解为字典)进行json格式的编码(转换成字符串,用于传播)
    eg,

    dict = {"age": "12"}
    json_str = json.dumps(dict)
    1
    2
    2. json.loads() 函数是将 json 格式数据(字符串)转换为字典(方便取出里面的数据),和 json.dumps() 正好相反
    eg,

    json_str = '{"age": "12"}'
    dict = json.loads(json_str)
    age = dict['age']
    3. json.dump() 函数用于将 json 信息(字符串)写进文件
    eg,

    json_str = "{'age': '12'}"
    file = open('1.json', 'w', encoding='utf-8')
    json.dump(json_str, file)
    4. json.load() 函数用于读取 json 信息(文件)转成字符串,和 json.dump() 相反
    eg,

    file = open('1.json', 'r', encoding='utf-8')
    info = json.load(file)
    ————————————————

  • 相关阅读:
    LA 3882
    Codeforces 161D
    spoj PARTIT
    uva 10496 Collecting Beepers
    uva 10130 SuperSale
    spoj 3885
    NWERC 2012 Problem I Idol
    NWERC 2012 Problem E Edge Case
    【USACO 1.3.2】修理牛棚
    【USACO 1.3.1】混合牛奶
  • 原文地址:https://www.cnblogs.com/valorchang/p/11474454.html
Copyright © 2011-2022 走看看