zoukankan      html  css  js  c++  java
  • anyjson与simplejson的使用

    import simplejson
    import anyjson
    
    # 序列化: 把变量从内存中变成可存储或可传输的过程
    
    def any_1():
        # 序列化
        info = {"a": 1, "b": 2}
        result = anyjson.serialize(info)
        f = open("file.text", "w")
        f.write(result)
        f.close()
    def any_2():
        # 反序列化
        with open("file.text", "r") as f:
            info = anyjson.deserialize(f.read())
        print(info)
    
    def smp_1():
        # 序列化
        # 方式一
        """info = {"a":1, "b": 2}
        result = simplejson.dumps(info)
        with open("122.text", "w") as f:
            f.write(result)"""
        # 方式二
        info = {"a": 1, "b": 2}
        with open("122.text", "w") as f:
            simplejson.dump(info, f)
    
    
    
    def simp_2():
        # 反序列化
        # 方式一
        """with open("122.text", "r") as f:
            info = f.read()
            result = simplejson.loads(info)
        print(result)"""
        # 方式二
        with open("122.text", "r") as f:
            info = simplejson.load(f)
        print(info)
    
    # 排序
    a = {"c": 1, "b": 2}
    info = simplejson.dumps(a, sort_keys=True)
    print(info)   # {"b": 2, "c": 1}
  • 相关阅读:
    24. Swap Nodes in Pairs
    49. Group Anagrams
    280. Wiggle Sort
    274. H-Index
    K Closest Numbers In Sorted Array
    Closest Number in Sorted Array
    Last Position of Target
    Classical Binary Search
    350. Intersection of Two Arrays II
    Sort Integers II
  • 原文地址:https://www.cnblogs.com/zhouzetian/p/13166245.html
Copyright © 2011-2022 走看看