迈向大神的day16 collections模块

collections 模块

  1. namedtuple 生成可以使用名字来访问元素内容 tuple

p=(1,2) # 点坐标

from collections import namedtuple
Point=namedtuple("point",['x','y'])
p=Point(1,2)
print(p.x)

eg :扑克牌

  1. deque --双端队列
import queue
q=queue.Queue()
q.put(15)
q.put(20)
print(q.get(())) #d多取不会报错 堵塞  
print(q.qsize()

hanyiming

from collections  import deque

deque.append()
deque.leftappend()#从前面放数据
deque.pop()
deque.popleft()  #从前面读数据
print()
  1. OrderedDict 有序列表

  2. defaultDict

    1. counter 跟踪值出现次数

eg c=Counter(‘12234455aaa’)

##时间模块

import  time
time.sleep()
time.time()

时间的3种表现形式

1.格式化时间

print(time.strftime("%Y-&m-%d %X"))   

%H:%M:%s

%j 多少天

2.时间戳i

import  time
time.time()

3.结构化时间 元祖

struct_time=time.locltime()

print(struct_time.tm_year)

时间戳 转化为

import time

print(time.localtime(1600000000))

random模块 --随机数

结构化 装换为时间戳

结构和格式化相互转换

print(time.strftime("%h",time.localtime(300000000)))

时间差 换成时间戳相减

随机数模块

import random

import random
random.random  #(0-1 之间的数)
random.uniform(0-3)   #0-3 之间的小数  

random.randint(1,5)  #包括5
random.randrange(1,10,2) #1-10 之间的奇数
random.choice([1,2,3])   #随机返回一个数

#打乱列表顺序    
item=[1,23,45,]
random.shuffle(item)
print(item)

exam 随机生成验证码

chr(65-90)

### os模块 与交互系统有关系

import os
os.getcwd()   #获取当前路径
os.chdir('123')#改变当前工作目录
os.curdir()   #返回当前路径
os.pardir()  #父目录字符串目录
os.makedirs(r'123\567')#多层级递归创建
os.removedirs()  #多层删除
os.listdir("c:")#列出指定目录的所有文件
os.rename('22',44)
os.stat("1.py")#获取文件信息
os.sep   # 系统分隔符   python 扩平台
os.system('cmd')   #运行shell 命令
ret=os.popen("dir").read()   #有返回值
print(ret)

os.environ()#环境变量
os.path.split("123")
os.path.exists("123")
os.path.isfile("123")#判断是不是文件
os.path.join(r"c:\",r"123")


sys模块—和python 解释器交互的一个接口

import sys
sys.exit(1)   #正常 0  错误1
print(sys.platform)

sys.version
sys.path   #模块所有路径
sys.argv

#
ret=sys.argv
name=ret[1]
name1=ret[2]    #终端才能用

##序列化

字典-字符串-》byte

数据传输

数据类型-》字符串的过程 序列化

字符串转换数据类型 反序列化

  1. json  —通用

    *只有很少一部分可以通过 dumps序列化方法  loads 反序列方法   内存中操作

    dic={'k':'v'}
    print(type(dic),dic)
    import json
    str_d=json.dump(dic)
    print(type(dic),dic)
    dic_d=json.loads(str_d)
    #数字 字符串 字典  列表  字典
    
    #dump load 文件操作
    
    
    import json
    dic={1:'a',2:'b'}
    f=open('ffff',,w,encoding="utf-8")
    json.dump(dic.f)
    json.dump(dic,f,ensure_ascii=False)   #中文解决办法
    json.load(f)
    f.close
    
    
    
    dic={'k':'v'}
    print(type(dic),dic)
    import json
    str_d=json.dump(dic)
    print(type(dic),dic)
    
    
    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+'\n')
    f.close()
    
    f = open('file')
    import json
    l = []
    for line in f:
        dic = json.loads(line.strip())
        l.append(dic)
    f.close()
    print(l)
    
  2. pickle

    所有python都可以转化 只有python能用且 反序列化依赖python代码 序列化任何类型

    #rb
    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()
    
    
    1. shelve

shelve 序列化句柄     使用句柄直接操作

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()

\

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值