码迷,mamicode.com
首页 > 编程语言 > 详细

python第五课

时间:2019-10-31 23:47:50      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:ash   fetchall   时间   hash   user   jin   decode   %s   uniq   

递归函数:自己调用自己,最多循环999次

def func():
num = int(input("请输入"))
if num %2 ==0:
print("是偶数")
else:
func()
func()



Lambda:匿名函数:执行完函数内存中就消失了,省内存

lambda x:x+1

result = 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() #打开一个excel
sheet = 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 = 0
for stu in stus:
col = 0
for filed in stu:
sheet.writ(row,col,filed)
col += 1
row += 1
book.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 xlrd
book = xlrd.open_workbook(‘students.xls‘) #打开读取的excel
sheet = 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相当于自动提交不需要commit
cur = 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 需增加commit
all = 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 all
info = {‘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 all
op_mysql(sql)


加密
MD5加密:md5加密是不可逆的,只能加密不能解密 无论加密内容有多长加密后都只有32位 sha256加密更长,更不容易破解
import hashlib
s = ‘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 += salt
m = 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())


redis
import redis 导入模块
# 数据库分为:关系型数据库:mysql\ oracle\sqlserver\sqlite
# 非关系型数据库(nosql) 速度快、存储量大、mongodb\redis
# redis每秒10万次读写,数据存在内存中

host = ‘118.24.3.40‘
port = 6379
password = ‘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() #获取当前数据库所有的key
r.type() #获取key的类型


迁移redis
r = redis.Redis(host=‘118.24.3.40‘,password=‘HK139bc&*‘,port=6379,db=0,decode_responses=True) 连接redis
r2 = 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() #执行管道

python第五课

标签:ash   fetchall   时间   hash   user   jin   decode   %s   uniq   

原文地址:https://www.cnblogs.com/wannn/p/11774400.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!