python --递归--匿名函数--枚举--xlwt模块--redis--mysql--加密解密
知识点一、递归
def fun(): num = int(input(‘输入内容‘)) if num%2 == 0: print(‘偶数‘) return else: fun()fun()
知识点二、匿名函数
#匿名函数#f = lambda x,b:str(x+b)#入参:返回值#(0,30)补零,map()循环函数,str(x).zfill(2)补零result = list(map(lambda x:str(x).zfill(2),range(1,30)))print(result)
#枚举内置函数l = [ 315, "矿泉水", "女", 27, "上海", "摩羯座", "18317155664", 100 ]for index,value in enumerate(l): print(‘%s-%s‘%(index,value))
知识点三、excel文件
1、写文件
1、1简单写入字符串
import xlwtimport xlutilsbook = xlwt.Workbook()#打开文件sheet = book.add_sheet(‘sheet1‘)#添加sheet页# sheet.write(0,0,‘名字‘)#(行,列,内容)# sheet.write(0,1,‘电话‘)# sheet.write(1,0,‘小航‘)# sheet.write(1,1,‘14489909‘)
book.save(‘student3.xls‘)1.2写入二维数组
import xlwtbook = xlwt.Workbook()#打开文件sheet = book.add_sheet(‘sheet1‘)#添加sheet页stu_list = [ [ "id", "name", "sex", "age", "addr", "grade", "phone", "gold" ], [ 314, "矿泉水", "男", 18, "北京市昌平区", "摩羯座", "18317155663", 14405 ], [ 315, "矿泉水", "女", 27, "上海", "摩羯座", "18317155664", 100 ], [ 5985, "矿泉水", "男", 18, "北京市昌平区", "班级", "18513867663", 100 ]]# row = 0#行# for stu in stu_list:#控制行# col = 0#列# for s1 in stu:#控制列# sheet.write(row,col,s1)# col += 1# row += 1### book.save(‘student2.xls‘)#枚举函数同上for row,stu in enumerate(stu_list): for col,s1 in enumerate(stu): sheet.write(row,col,s1)book.save(‘student3.xls‘)
2、读文件
import xlrdbook = xlrd.open_workbook(‘student3.xls‘)sheet = book.sheet_by_index(0)result = sheet.cell(0,0).value#单元格内容print(result)row = sheet.row_values(0)#整行内容print(‘行‘,row)col = sheet.col_values(0)#整列内容print(‘列‘,col)print(sheet.nrows)#总行数print(sheet.ncols)#总列数for row_num in range(sheet.nrow): print(sheet.row_values(row_num))知识点四、操作数据库
import pymysqlip = ‘118.24.*.*‘#需要写可以连接的ipuser = ‘***‘password = ‘******‘db = ‘***‘port = 3306#默认端口号,可以不指定,需要改的话要传charset = ‘utf8‘conn= pymysql.connect(host = ip,user = user,password = password,db = db,port = port ,charset = charset,autocommit=True)#连接数据库#autocommit = True 是否自动提交
cur= conn.cursor()#游标#sql = ‘select *from app_myuser limit 10;‘#创建表‘‘‘create table ymy (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");‘‘‘‘‘‘insert into mjz (id,name,phone) values (2,"小白2","17812343211");‘‘‘#修改语句‘‘‘update mjz set name="小红" where id =1;‘‘‘#删除语句‘‘‘delete from mjz where id=3;‘‘‘cur.execute(sql)#执行sql语句conn.commit()#insert,update,delete,需要加这个语句才能同步到数据库one = cur.fetchone()#fetchone()函数它的返回值是单个的元组,也就是一行记录,如果没有结果,那就会返回Nonemany = cur.fetchmany()#获取指定数量结果fetmany(x)all = cur.fetchall()#fetchall()函数,它的返回值是多个元组,即返回多个行记录,如果没有结果,返回的是()cur.close()#关游标conn.close()#关连接print(one)print(many)print(all)
知识点五、redis 操作
1、字符串操作
import redis#关系型数据库#mysql oracle sqlserver、sqlite#非关系型数据库 NoSQL# mongodb# redis 每秒钟达到10w次的读写,存在内存里面r = redis.Redis(host=‘118.24.*.*‘,password=‘H***‘,port=6379,db=0,decode_responses=True)#字符串r.set(‘mjz_students‘,‘{"msg":"sdfsdfsd"}‘)info = r.get(‘mjz_students‘)print(info)r.expire(‘dabaobao‘,30)#设置失效时间r.delete(‘mjz_students‘)#删除r.set(‘mjz_students‘,‘{"msg":"sdfsdfsd"}‘)
2、哈希类型
r.hset("students",‘wyj‘,‘{"money":500,"addr":"北京"}‘)r.hset("students",‘cj‘,‘{"money":500,"addr":"上海"}‘)r.hset("students",‘dcg‘,‘{"money":502,"addr":"北京"}‘)r.hset("students",‘wn‘,‘{"money":502,"addr":"北京"}‘)r.hset("students",‘wyj‘,‘{"money":502,"addr":"北京"}‘)## # r.hdel("students",‘wyj‘)#删除# # print(r.hget("students","wn"))# # print(r.hgetall(‘students‘)) #获取所有的## d = {‘qxh‘:‘sfdsdfsf‘,‘liuying‘:‘liy001‘}# r.hmset(‘students‘,d)
#r.flushall() #清空所有数据库的所有内容#r.flushdb() #清空当前数据库里面的数据#r.exists(‘name‘) #判断key是否存在# r.keys() #获取当前数据库所有的key# r.type(‘name‘) #获取的key的类型redis迁移
#1、从aredis里面获取所有的key,#2、判断key的类型#3、根据key的类型,使用set /hset#4、set到bredis里面import timeimport redisr = redis.Redis(host=‘118.24.3.4**‘,password=‘*****‘,port=6379,db=0,decode_responses=True)r2 = redis.Redis(host=‘118.24***‘,password=‘*****‘,port=6378,db=0,decode_responses=True)for k in r.keys(): if r.type(k)==‘string‘: value = r.get(k) r2.set(k,value) if r.type(k)==‘hash‘: value = r.hgetall(k) r2.hmset(k,value)#3、管道
l = range(500)start_time = time.time()pipeline = r.pipeline() #建立一个管道for i in l: pipeline.set("key%s"%i,str(i)) # r.set("key%s"%i,str(i))pipeline.execute() #执行管道print(time.time() - start_time)知识点六、加密解密1、md5:加密不可逆,可以加密,不能解密
import hashlibs=‘123456‘m = hashlib.md5( s.encode())#md5加密#m = hashlib.sha224( s.encode() )#加密长度为224result = m.hexdigest() #获取加密后的结果print(result)
# #撞库 #加盐:增加密码复杂度
import hashlib
salt=‘#@#@$‘password = input(‘password:‘)password += saltm = hashlib.md5( password.encode() )result = m.hexdigest() #获取加密后的结果print(result)
#加盐函数
import hashlib
def md5(s,salt=‘‘): new_s = str(s) + salt m = hashlib.md5(new_s.encode()) return m.hexdigest()result = md5("67888")print(result)
base64加密,可以解密
import base64 #能加密,也能解密s=‘https://www.cnblogs.com/zanjiahaoge666/p/7242642.html‘b = base64.b64encode( s.encode() ) #加密result= b.decode()print(result)
import base64
b = base64.b64decode( ‘5ZOI5ZOI5ZOI5ZOI‘ ) #解密print(b.decode())
相关推荐
举 2020-05-20
大地飞鸿 2020-11-12
steeven 2020-11-10
匆匆那些年 2020-10-15
Tips 2020-10-14
nongfusanquan0 2020-08-18
yedaoxiaodi 2020-07-26
夜晚00 2020-07-03
hanyujianke 2020-06-28
xhao 2020-06-28
清溪算法君老号 2020-06-27
pengkingli 2020-06-25
yishujixiaoxiao 2020-06-25
Masimaro 2020-06-21
清溪算法 2020-06-21
oXiaoChong 2020-06-20
RememberMePlease 2020-06-17
chaigang 2020-06-13