• day20序列化和模块导入


    #序列化:将原本的字典、列表等内容转换成一个字符串的过程就叫做序列化。
    # 序列化 —— 转向一个字符串数据类型
    # 序列 —— 字符串
    "{'k':'v'}"
    # 数据存储
    # 网络上传输的时候

    # 从数据类型 --> 字符串的过程 序列化
    # 从字符串 --> 数据类型的过程 反序列化
    # json *****
    # pickle ****
    # shelve ***

    # json # 数字 字符串 列表 字典 元组
    # 通用的序列化格式
    # 只有很少的一部分数据类型能够通过json转化成字符串
    # pickle
    # 所有的python中的数据类型都可以转化成字符串形式
    # pickle序列化的内容只有python能理解
    # 且部分反序列化依赖python代码
    # shelve
    # 序列化句柄
    # 使用句柄直接操作,非常方便

    # json dumps序列化方法 loads反序列化方法
    # import json
    # dic = (1,2,3,4)#{'k1':'v1'}
    # print(type(dic))
    # str_d = json.dumps(dic) # 序列化
    # print(type(str_d),str_d)
    # # # '{"kkk":"v"}'
    # dic_d = json.loads(str_d) # 反序列化
    # print(type(dic_d),dic_d)
    #
    #
    # import json
    # #json dump load
    # dic = {1:"a",2:'b'}
    # f = open('fff','w',encoding='utf-8')
    # json.dump(dic,f)
    # f.close()
    # f = open('fff')
    # res = json.load(f)
    # f.close()
    # print(type(res),res)

    # import json
    # # json dump load
    # dic = {1:"中国",2:'b'}
    # f = open('fff','w',encoding='utf-8')
    # json.dump(dic,f,ensure_ascii=False)
    # json.dump(dic,f,ensure_ascii=False)
    # f.close()
    # f = open('fff',encoding='utf-8')
    # res1 = json.load(f)
    # res2 = json.load(f)
    # f.close()
    # print(type(res1),res1)
    # print(type(res2),res2)


    # l = [{'k':'111'},{'k2':'111'},{'k3':'111'}]
    # f = open('file','w')
    # import json
    # for dic in l:
    # str_dic = json.dumps(dic)
    # f.write(str_dic+' ')
    # f.close()

    import pickle
    # dic = {'k1':'v1','k2':'v2','k3':'v3'}
    # str_dic = pickle.dumps(dic)
    # print(str_dic) #一串二进制内容
    #
    # dic2 = pickle.loads(str_dic)
    # print(dic2) #字典

    import time
    struct_time1 = time.localtime(1000000000)
    struct_time2 = time.localtime(2000000000)
    # f = open('pickle_file','wb')
    # pickle.dump(struct_time1,f)
    # pickle.dump(struct_time2,f)
    # f.close()
    # f = open('pickle_file','rb')
    # struct_time1 = pickle.load(f)
    # struct_time2 = pickle.load(f)
    # print(struct_time1.tm_year)
    # print(struct_time2.tm_year)
    # f.close()



    # import shelve
    # f = shelve.open('shelve_file')
    # f['key'] = {'int':10, 'float':9.5, 'string':'Sample data'} #直接对文件句柄操作,就可以存入数据
    # f.close()
    #
    # import shelve
    # f1 = shelve.open('shelve_file')
    # existing = f1['key'] #取出数据的时候也只需要直接用key获取即可,但是如果key不存在会报错
    # f1.close()
    # print(existing)


    # import shelve
    # f = shelve.open('shelve_file', flag='r')
    # existing = f['key']
    # print(existing)
    # f.close()
    #
    # f = shelve.open('shelve_file', flag='r')
    # existing2 = f['key']
    # f.close()
    # print(existing2)

    #想要改文件的话必须设置writeback=True
    import shelve
    f1 = shelve.open('shelve_file')
    print(f1['key'])
    f1['key']['new_value'] = 'this was not here before'
    f1.close()

    f2 = shelve.open('shelve_file', writeback=True)
    print(f2['key'])
    f2['key']['new_value'] = 'this was not here before'
    f2.close()

    f2 = shelve.open('shelve_file', writeback=True)
    print(f2['key'])
    # f2['key']['new_value'] = 'this was not here before'
    f2.close()

    import demo
    demo.read()
    print(demo.money)
    #同一个模块,导入多次他也只执行一次
    # 文件
    # import demo
    # def read():
    # print('my read func')
    # demo.read()
    # print(demo.money)
    # 先从sys.modules里查看是否已经被导入
    # 如果没有被导入,就依据sys.path路径取寻找模块
    # 找到了就导入
    # 创建这个模块的命名空间
    # 执行文件,把文件中的名字都放到命名空间里
    import sys
    print(sys.modules.keys())
    print(sys.path)

    import time as t
    print(t.time())
    # oracle
    # mysql
    # if 数据库 == ‘oracle’:
    # import oracle as db
    # elif 数据库 == ‘mysql’:
    # import mysql as db
    # # 连接数据库 db.connect
    # # 登录认证
    # # 增删改查
    # # 关闭数据库

    # import time,sys,os

    from time import sleep
    from demo import read
    def read():
    print('my read')
    read()
    #使用from demo import read的这种形式,如果从新定义了和read相同的名字,就会覆盖掉之前导入的模块,而import demo这种形式不存在该问题
    from demo import money,read
    # print(money)
    # read()
    money = 200
    read()

    from time import *
    # sleep = 10
    sleep(1)

    from math import pi
    print(pi)
    pi = 3
    print(pi)
    #模块导入的时候用from 模块名 import 变量名 的时候如果模块里定义了__all__ = ['read','read2','login'],定义了的变量才能被调用
    # 所有的模块导入都应该尽量往上写
    # 内置模块
    # 扩展模块
    # 自定义模块
    # 模块不会重复被导入 : sys.moudles
    # 从哪儿导入模块 : sys.path
    #import
    # import 模块名
    # 模块名.变量名 和本文件中的变量名完全不冲突
    # import 模块名 as 重命名的模块名 : 提高代码的兼容性
    # import 模块1,模块2

    #from import
    # from 模块名 import 变量名
    #直接使用 变量名 就可以完成操作
    #如果本文件中有相同的变量名会发生冲突
    # from 模块名 import 变量名字 as 重命名变量名
    # from 模块名 import 变量名1,变量名2
    # from 模块名 import *
    # 将模块中的所有变量名都放到内存中
    # 如果本文件中有相同的变量名会发生冲突
    # from 模块名 import * 和 __all__ 是一对
    # 没有这个变量,就会导入所有的名字
    # 如果有all 只导入all列表中的名字
    # __name__
    # 在模块中 有一个变量__name__,
    # 当我们直接执行这个模块的时候,__name__ == '__main__'
    # 当我们执行其他模块,在其他模块中引用这个模块的时候,这个模块中的__name__ == '模块的名字'


  • 相关阅读:
    1250. Check If It Is a Good Array
    380. Insert Delete GetRandom O(1)
    378. Kth Smallest Element in a Sorted Matrix
    341. Flatten Nested List Iterator
    387. First Unique Character in a String
    454. 4Sum II
    D
    勇敢的妞妞 ( 状压 + 思维)
    P1879 [USACO06NOV]玉米田Corn Fields (状压dp入门)
    G
  • 原文地址:https://www.cnblogs.com/Murraya/p/11027074.html
Copyright © 2020-2023  润新知