zoukankan      html  css  js  c++  java
  • python学习day20

    #序列化模块
    #序列化——转向一个字符串数据类型
    #序列——字符串
    #用途:
    #数据存储
    #网络传输
    # 数据类型——>字符串的过程 序列化
    #字符串——>数据类型的过程 反序列化
    #三种模块:json,pickle,shelve
    #json:数字,字符串,列表,字典,元组
    #通用的序列化格式
    #自能支持很少的一部分数据类型

    #pickle:所有的puthon中的数据类型都可转化
    #pickle序列化的内容只有python能理解
    #切.部分反序列化依赖python代码

    #shelve:
    #序列化句柄
    #直接使用句柄操作,十分方便
    #json dumps序列化方法 loads反序列化方法
    dic = {1:'q',2:'d'}
    print(type(dic),dic)
    # import json
    # str_dic = json.dumps(dic) #序列化
    # print(type(str_dic),str_dic)
    # dic_d = json.loads(str_dic) #反序列化
    # 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)

    # json
    # dumps {} -- > '{} '
    # 一行一行的读
    # '{} '
    # '{}' loads
    # 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()

    # f = open('file')
    # import json
    # l = []
    # for line in f:
    # dic = json.loads(line.strip())
    # l.append(dic)
    # f.close()
    # print(l)

    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)

    # 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()
    # 内置模块
    # 扩展的 django
    # 自定义的

    # 文件
    # 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()

    # import demo
    # from demo import 变量名

    # from demo import money,read
    # # print(money)
    # # read()
    # money = 200
    # read()
    # 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 demo import *
    # print(money)
    # read()

    # import demo
    # print(demo.money)

    # 所有的模块导入都应该尽量往上写
    # 内置模块
    # 扩展模块
    # 自定义模块
    # 模块不会重复被导入 : 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__ == '模块的名字'
  • 相关阅读:
    移植Linux2.6.38到Tiny6410_1GNandflash
    【转载】Mini6410启动过程
    【转载】Mini2440启动配置文件说明
    【转载】linux2.6内核initrd机制解析
    第十三章 StringTable
    第十二章 执行引擎
    第十一章 直接内存(Direct Memory)
    第十章 对象的实例化、内存布局与访问定位
    第九章 方法区
    第八章 堆
  • 原文地址:https://www.cnblogs.com/wujunjie-sir/p/9228544.html
Copyright © 2011-2022 走看看