zoukankan      html  css  js  c++  java
  • Python Json

    import json
    
    lt = [
        {'name': '王宝强', 'age': 30},
        {'name': '贾乃亮', 'age': 36},
        {'name': '马蓉蓉', 'age': 33},
        {'name': '宋吉吉', 'age': 40},
        {'name': '李小璐', 'age': 43},
    ]
    # json字符串<------dumps------python数据类型
    #          ------loads------->
    # 包含json的类文件对象<------dump------python数据类型
    #                   ------load------->
    # 具有read()或者write()方法的对象就是类文件对象
        # f = open(“a.txt”,”r”) f就是类文件对象
    string = json.dumps(lt)
    print(type(string))  # <class 'str'>  # json字符串
    print(string)  # [{"name": "u738bu5b9du5f3a", "age": 30}, {"name": "u8d3eu4e43u4eae", "age": 36}, {"name": "u9a6cu84c9u84c9", "age": 33}, {"name": "u5b8bu5409u5409", "age": 40}, {"name": "u674eu5c0fu7490", "age": 43}]
    obj = json.loads(string)
    
    print(type(obj))  # <class 'list'>
    
    json.dump(lt, open('json.txt', 'w', encoding='utf8'))
    obj = json.load(open('json.txt', 'r', encoding='utf8'))
    print(type(obj))  # <class 'list'>
    print(obj)  # [{'name': '王宝强', 'age': 30}, {'name': '贾乃亮', 'age': 36}, {'name': '马蓉蓉', 'age': 33}, {'name': '宋吉吉', 'age': 40}, {'name': '李小璐', 'age': 43}]
    
    
                  
    

      

  • 相关阅读:
    开发软件设计模型 visual studio UML
    to debug asp.net mvc4
    BeeFramework
    .net entity framework 泛型 更新与增加记录
    javascript debut trick, using the throw to make a interrupt(breakpoint) in your program
    C# dynamic
    webapi
    C# async / await
    NYoj 613 免费馅饼
    洛谷P1056:排座椅(贪心)
  • 原文地址:https://www.cnblogs.com/yzg-14/p/12121863.html
Copyright © 2011-2022 走看看