zoukankan      html  css  js  c++  java
  • 爬虫17-json用法

    1.dump

    import json
    persons=[
        {
            'username':"wangchenyang",
            'age':14,
            'country':"china"
        },
        {
            'username':"王晨阳",
            'age':14,
            'country':"china"
        }
    ]
    # json_str=json.dumps(persons)
    # print(json_str)
    with open('person.json','w',encoding="utf-8") as fp:
        json.dump(persons,fp,ensure_ascii=False)
    

      

    2.load

    import json
    
    # json_str='[{"username": "wangchenyang", "age": 14, "country": "china"}, {"username": "王晨阳", "age": 14, "country": "china"}]'
    
    # persons=json.loads(json_str)
    # for person in persons:
    #     print(person)
    
    with open('person.json','r',encoding="utf-8") as fp:
        persons = json.load(fp)
        for person in persons:
            print(person)
    

      

  • 相关阅读:
    20210603
    20210602
    20210601
    20210531-已编辑
    2021053101
    操作系统笔记(一)
    尘埃落定,扬帆起航
    RTL级低功耗设计
    关于毛刺
    电路级拾珍
  • 原文地址:https://www.cnblogs.com/wcyMiracle/p/12490177.html
Copyright © 2011-2022 走看看