python第五课
递归函数:自己调用自己,最多循环999次def func(): num = int(input("请输入")) if num %2 ==0: print("是偶数") else: func()func()Lambda:匿名函数:执行完函数内存中就消失了,省内存 lambda x:x+1result = list(map(lambda x:str(x).zfill(3),range(0,30)))print(result)enumerate 枚举函数l = [‘id‘, ‘name‘, ‘sex‘, ‘age‘, ‘addr‘, ‘grade‘, ‘phone‘, ‘gold‘]for index,value in enumerate(l,-1): print(‘%s--%s‘%(index,value))
excel操作:写excel:import xlrd #导入模块book = xlrd.open_workbook() #打开一个excelsheet = book.add_sheet("sheet1") # 建立一个sheet页sheet.writ(0,0,‘nihao‘) # 输入写的内容book.save(‘hello.xls‘) # 保存写的excel例子:将一下内容写入excel中stus = [[‘id‘, ‘name‘, ‘sex‘, ‘age‘, ‘addr‘, ‘grade‘, ‘phone‘, ‘gold‘], [314, ‘矿泉水‘, ‘男‘, 18, ‘北京市昌平区‘, ‘摩羯座‘, ‘18317155663‘, 14405], [315, ‘矿泉水‘, ‘女‘, 27, ‘上海‘, ‘摩羯座‘, ‘18317155664‘, 100], [5985, ‘矿泉水‘, ‘男‘, 18, ‘北京市昌平区‘, ‘班级‘, ‘18513867663‘, 100]]方法一:row = 0for stu in stus: col = 0 for filed in stu: sheet.writ(row,col,filed) col += 1 row += 1book.save(‘stu.xls‘)方法二:for row,stu in enumerate(stus): for col,filed in enumerate(stu): print(row,col,filed)book.save(‘stu.xls‘)读excel:import xlrdbook = xlrd.open_workbook(‘students.xls‘) #打开读取的excelsheet = book.sheet_by_index(0) #读取某个sheet页的内容,按照sheet页下表读取# sheet = book.sheet_by_name(‘我的‘) #读取某个sheet页的内容,按照sheet页名称读取result = sheet.cell(0,0) #获取某个单元格的数据raw = sheet.row_values(1) #获取某行col = sheet.col_values(1) #获取某列print(sheet.nrows) #获取行数print(sheet.ncols) #获取列数for row_num in range(sheet.nrows): print(sheet.row_values(row_num)) #获取所有数据操作数据库:import pymysql#连接数据库connect = pymysql.connect(host=ip,user=user,password=password,db=db,port=3306,charset = ‘utf8‘,autocommit=True) #autocommit=True相当于自动提交不需要commitcur = connect.cursor() #建立游标,仓库管理员cur = connect.cursor(pymysql.cursors.DictCursor) #游标,仓库管理员 ----字典形式展示sql = ‘select * from wnn limit 3;‘ #SQL语句 (新增表 create table mjz (id int unique not null, name varchar(20) not null, phone varchar(11) unique not null); 增加语句 insert into mjz (id,name,phone) values (1,"小白","19812343211"); 更新语句 update mjz set name="春光" where id =1; 删除语句 delete from mjz where id=3;)cur.execute(sql) #执行生SQL语句(只执行SQL语句,拿不到结果)# connect.commit() #insert\update\delete 需增加commitall = cur.fetchall() #获取全部数据print(all)one = cur.fetchone() #获取一条数据print(one)many = cur.fetchmany(1) #获取传入数据的数量print(many)cur.close() #关闭游标connect.close() #关闭数据库数据库操作相关函数:传入SQL以及配置文件def op_mysql(sql,db_info): connect = pymysql.connect(db_info) # outcommit=True 相当于自动提交不需要commit cur = connect.cursor(pymysql.cursors.DictCursor) cur.execute(sql) all = cur.fetchall() cur.close() connect.close() return allinfo = {‘user‘:‘name‘,‘password‘:‘password‘,‘host‘:‘110.12.0.3‘,‘db‘:‘db‘,‘port‘:3306,‘charset‘:‘utf8‘,‘autocommit‘:True}op_mysql(sql,info)配置文件固定时,之传入SQL语句def op_mysql(sql): db_info = {‘user‘: ‘name‘, ‘password‘: ‘password‘, ‘host‘: ‘110.12.0.3‘, ‘db‘: ‘db‘, ‘port‘: 3306, ‘charset‘: ‘utf8‘, ‘autocommit‘: True} connect = pymysql.connect(db_info) # outcommit=True 相当于自动提交不需要commit cur = connect.cursor(pymysql.cursors.DictCursor) cur.execute(sql) all = cur.fetchall() cur.close() connect.close() return allop_mysql(sql)加密MD5加密:md5加密是不可逆的,只能加密不能解密 无论加密内容有多长加密后都只有32位 sha256加密更长,更不容易破解import hashlibs = ‘123‘m = hashlib.md5(s.encode()) s.encode()规定加密的时候只能传bais类型(二进制类型)m = hashlib.sha256(s.encode())resut = m.hexdigest() #获取加密后的结果print(resut)加盐salt= ‘76747y74hghrhg‘password = input("password")password += saltm = hashlib.md5(password.encode())resut = m.hexdigest()print(resut)加密函数def md5(s,salt=‘‘): new_s = str(s) + salt m = hashlib.md5(new_s.encode()) resut = m.hexdigest() print(resut)md5(‘jkdjfij‘)import base64 #可加密可解密,加密最后有个=# 加密s = ‘hahahahha‘b = base64.b64encode(s.encode())print(b.decode())# 解密b = base64.b64decode(‘aGFoYWhhaGhh‘)print(b.decode())redisimport redis 导入模块# 数据库分为:关系型数据库:mysql\ oracle\sqlserver\sqlite# 非关系型数据库(nosql) 速度快、存储量大、mongodb\redis# redis每秒10万次读写,数据存在内存中host = ‘118.24.3.40‘port = 6379password = ‘HK139bc&*‘# 字符串类型r = redis.Redis(host = ‘118.24.3.40‘,port = 6379,password = ‘HK139bc&*‘,db=0,decode_responses=True) #连接redis,decode_responses=True自动变成字符串r.set(‘wifi‘,‘fre4f546‘,20) #修改,第三个参数(设置失效时间),设置的话KEY的生效时间等于第三个参数,到时间redis自动删掉key,不设置默认永久生效print(r.get(‘wifi‘).decode()) #获取数据.decode()变成字符串# r.expire(‘wifi‘,30) #设置失效时间# r.delete(‘wifi‘) #删除数据#哈希类型r.hset(‘wifi‘,‘123456‘,{‘money‘:500,‘add‘:‘beijing‘})r.hdel(‘wifi‘,‘123456‘) #删除print(r.hge(‘wifi‘)) #获取某个print(r.hgetall(‘wifi‘)) #获取所有r.flushall() #清空所有的数据库所有内容r.flushdb() #清空当前数据库的数据r.exists() #判断key是否存在r.keys() #获取当前数据库所有的keyr.type() #获取key的类型迁移redisr = redis.Redis(host=‘118.24.3.40‘,password=‘HK139bc&*‘,port=6379,db=0,decode_responses=True) 连接redisr2 = redis.Redis(host=‘118.24.3.40‘,password=‘HK139bc&*‘,port=6378,db=0,decode_responses=True)print(r.keys())for key in r.keys(): if r.type(key)==‘string‘: value = r.get(key) r2.set(key,value) if r.type(key)==‘hash‘: value = r.hgetall(key) r2.hmget(key,value)管道 提升性能的pipeline = r.pipeline() #建立一个管道l = range(500)for i in l: pipeline.set(‘key%s‘%i,str(i))pipeline.execute() #执行管道
相关推荐
zhangchaoming 2020-05-17
ipqtjmqj 2020-04-21
Yyqingmofeige 2020-03-23
jzlixiao 2020-03-06
sdwylry 2020-02-20
chichichi0 2020-01-07
云之高水之远 2020-01-03
糊一笑 2020-01-02
huakai 2019-12-23
tvk 2019-12-19
waitwolf 2019-12-07
torrent0 2019-11-17
zxznsjdsj 2019-11-04
86427019 2019-10-29
laxexue 2019-09-06
yuelcay 2019-09-06
秋草正离离 2019-04-16