• 21 python 初学(json pickle shelve)


    json:

    # _author: lily
    # _date: 2019/1/19
    
    import json
    
    my_dict = {'name': 'lily', 'age': 18}
    f = open('JSON_text', 'w')
    
    # data = json.dumps(my_dict)
    # f.write(data)
    # 上面两句等价于
    json.dump(my_dict, f)
    
    f.close()
    # _author: lily
    # _date: 2019/1/20
    
    import json
    
    f = open('JSON_text', 'r')
    
    # data = f.read()
    # my_dict = json.loads(data)
    # 上面两句话等价于
    my_dict = json.load(f)
    
    print(my_dict['name'])
    f.close()

    pickle:

    # _author: lily
    # _date: 2019/1/20
    
    import pickle
    
    def foo():
        print('ok')
    
    data = pickle.dumps(foo)
    f = open('PICKLE_text', 'wb')
    f.write(data)
    f.close()
    # _author: lily
    # _date: 2019/1/20
    
    import pickle
    
    def foo():
        print('okok')
    
    f = open('PICKLE_text', 'rb')
    data = f.read()
    
    data = pickle.loads(data)
    
    data()
    
    f.close()

    shelve:

    # _author: lily
    # _date: 2019/1/20
    
    import shelve
    
    # f = shelve.open('SHELVE_text')
    # f['info'] = {'name': 'lily', 'age': 18}
    
    
    f = shelve.open('SHELVE_text')
    data = f.get('info')
    print(data['name'])
    猪猪侠要努力呀!
  • 相关阅读:
    遍历Map的四种方法
    过滤器和拦截器的区别
    拦截器、过滤器做什么的?
    Linux中常用操作命令
    什么是反射?
    得到字节码的方式有哪三种?
    web考试内容1
    spring注入方式
    PHP基础 第三天
    堆和栈,值传递和地址传递(引用传递)
  • 原文地址:https://www.cnblogs.com/mlllily/p/10294900.html
Copyright © 2020-2023  润新知