Day6-os模块-datetime的练习-map()/filter()
import datetime print(datetime.datetime.today()) print(datetime.datetime.today()+datetime.timedelta(-1))#一天前 print(datetime.date.today()) print(datetime.date.today()+datetime.timedelta(-1)) print(datetime.datetime.today()+datetime.timedelta(hours=10))#10个小时之后 print(datetime.datetime.today()+datetime.timedelta(hours=-1,minutes=-20)) ''' 需要注意的地方就是类型是datetime.date,如果有需要可以先转换为str类型再使用 res = datetime.date.today() # res = str(res) print(type(res))#<class 'datetime.date'> print(res) '''
# 2、写一个程序,创建一些数据。 # 1、创建10个文件夹,文件夹名字自定义 # 2、每个文件下面有10个 日志文件, # 文件名是从今天开始的前10天 # android_2018-07-01.log # android_2018-06-30.log # android_2018-06-29.log # android_2018-06-28.log # android_2018-06-28.log # android_2018-06-28.log # android_2018-06-28.log # 3、随机选3个文件,往里面写点东西 import os import datetime import random BASE_PATH = r'C:\Users\Administrator\Desktop\pylearn_spz\Day6\logs' for i in range(5): dir_name = 'Android_log%s'%i dir_path = os.path.join(BASE_PATH,dir_name) os.makedirs(dir_path) for n in range(5): ctime = str(datetime.date.today()+datetime.timedelta(-n)) fname = 'android'+ctime+'.log' fname_path = os.path.join(dir_path,fname) open(fname_path,'w') luckfile = random.sample(os.listdir(dir_path),3) for f in luckfile: abs_file_name = os.path.join(dir_path, f) open(abs_file_name, 'w',encoding='utf-8').write('你很幸运')
'''描述 map() 会根据提供的函数对指定序列做映射。 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。 语法 map() 函数语法: map(function, iterable, ...) 参数 function -- 函数,有两个参数 iterable -- 一个或多个序列 返回值 Python 2.x 返回列表。 Python 3.x 返回迭代器。 ''' import os def make_dir(dirname): if not os.path.isdir(dirname): os.mkdir(dirname) return True else: print('文件夹已经存在') res = list(map(make_dir,['try1','try2']))#这里如果不转成List 返回的是迭代器 print(res)
'''描述 filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表。 该接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为参数传递给函数进行判, 然后返回 True 或 False,最后将返回 True 的元素放到新列表中。 语法 以下是 filter() 方法的语法: filter(function, iterable) 参数 function -- 判断函数。 iterable -- 可迭代对象。 ''' def use_filter(num): if num%2 == 0: print('偶数%s'%num) else: print('奇数%s'%num) res1 = list(filter(use_filter,[1,4,6,7])) print(res1)
#Excel 表格的操作
import xlwt book = xlwt.Workbook()#建一个excel 表 sheet = book.add_sheet('stu_info')#建一个表单 sheet.write(0,0,'呵呵')#写点东西 book.save('stu.xls')#保存
#发邮件
import yagmail #连邮箱 username = '×××' passwd ='××××'#什么码来着? mail = yagmail.SMTP(user=username,password=passwd,host='smtp.163.com') #如果是qq邮箱就加一个参数 smtp_ssl = True mail.send(to = ['996638257@qq.com','uitestp4p@163.com'], cc='409308045@qq.com',subject='0708水瓶座带附件', contents='嘿嘿嘿', attachments=r'C:\Users\Administrator\Desktop\pylearn_spz\作业视频.py' )
#Random
import random print(random.random())#只能取小于1的随机小数 for i in range(10): print(random.random()) print(random.randint(1,10))#指定范围取一枚随机整数 头和尾都能取到 s = 'a,b,c' print(random.choice(s))#随机选择一个 print(random.sample(s,3)) print(random.uniform(1,10))#取制定范围的一个随机小数
数据库 # #1.连数据库 Ip账号密码 端口号 数据库 # #2.执行sql # #3.获取结果 # # import pymysql # #建立数据库连接 # conn = pymysql.connect(host='118.24.3.40',user='...', # password='......', # port=3306,db='jxz',charset='utf8',autocommit=True) # # cur = conn.cursor()#建立游标 # # cur.execute('show tables;')#执行sql语句execute # sql = 'insert into nhy (name,pwd) value("Zhangjing","123456");' # cur.execute(sql) # # conn.commit() # cur.execute('select * from nhy where name = "zhangjing";') # print(cur.fetchall())#获取所有结果 # # print(cur.fetchone())#只获取一条 # # print(cur.fetchmany(2))#指获取几条 # cur.close()#关闭游标 # conn.close()#连接关闭 def my_db(ip,user,passwd,db,sql,port=3306,charset='utf8'): conn = pymysql.connect(host=ip,user=user,password=passwd, db=db,port=port,charset=charset, autocommit=True) cur = conn.cursor() cur.execute(sql) sql = sql.strip() sql_start = sql[:6].lower() if sql_start.starstwith('select') or sql_start.starstwith('show'): data = cur.fetchall() else: data = 'ok' cur.close() conn.close() return data
#Redis #传统的关系数据库 mysql oracle sql server sqllie db2 #非关系数据库 nosql 没有表结构 mongodb 数据存在磁盘上 redis 数据都是存在内存里 import redis #连接redis r = redis.Redis(host='...', password='H...', db=10) 增删改查 添加修改都用set r.set('zhangjing','5,4,3')#增加数据 res = r.get('zhangjing') print(res.decode())#将二进制编码变成了字符串 print(r.get('zhangjing'))#获取数据 #b'5,4,3' 返回的数值里面前面多个b 代表二进制 print(r.get('bucunzai').decode()) /# #get 一个不存在的Key返回的是None decode 之后会报错 r.delete('oxing') print(r.keys())#获取到所有的Key print(r.keys('*info')) #获取以info 结尾的数据 # print(r.exists('zj'))#判断这个key是否存在 # r.flushdb()#清空当前db中所有的Key redis命令行里面 切换数据库 select 10 r.hset('Try','20180708','beijing') r.hdel('session_crm','zhangjing')#删除制定的小Key r.delete('session_crm')#直接删除大key r.pexpire('key',600)#设置失效时间 600s print(r.ttl('session_crm')) #用来看这个key 的失效时间 print(r.type('session_crm'))#查看key 的类型 # print(r.hget('session_crm','zhangjing'))#获取制定小key 对应的数据 print(r.hgetall('session_crm')) Org = r.hgetall('Try') print(Org) a = {} for k,v in Org.items(): # print('key',k) # print('va',v) a[k.decode()] = v.decode() print(a) for k,v in Org.items(): Org[k.decode()] = Org.pop(k).decode() pop 返回的是对应的value print(Or