zoukankan      html  css  js  c++  java
  • python 处理json

    1、dumps:将python中的 字典 转换为 字符串

    import json
    
    test_dict = {'bigberg': [7600, {1: [['iPhone', 6300], ['Bike', 800], ['shirt', 300]]}]}
    print(test_dict)
    print(type(test_dict))
    #dumps 将数据转换成字符串
    json_str = json.dumps(test_dict)
    print(json_str)
    print(type(json_str))

    2、loads: 将 字符串 转换为 字典

    new_dict = json.loads(json_str)
    print(new_dict)
    print(type(new_dict))

    3、dump: 将数据写入json文件中

    with open("../config/record.json","w") as f:
         json.dump(new_dict,f)
         print("加载入文件完成...")

    4、load:把文件打开,并把字符串变换为数据类型

    with open("../config/record.json",'r') as load_f:
        load_dict = json.load(load_f)
        print(load_dict)
    load_dict['smallberg'] = [8200,{1:[['Python',81],['shirt',300]]}]
    print(load_dict)
    
    with open("../config/record.json","w") as dump_f:
        json.dump(load_dict,dump_f)

    5、遍历key value

    test_json = {"a":1,"b":2,"c":3}
    for key,value in test_json.items():
        print key,value
    联系方式:emhhbmdfbGlhbmcxOTkxQDEyNi5jb20=
  • 相关阅读:
    DBHelper
    jsTree使用
    爬虫系列之Scrapy框架
    Mongodb安装
    爬虫系列之mongodb
    爬虫学习目录
    爬虫之selenium模块
    爬虫简介与requests模块
    爬虫数据解析的三方式
    线程相关
  • 原文地址:https://www.cnblogs.com/zl1991/p/14373529.html
Copyright © 2011-2022 走看看