day5学习之内置函数、lambda匿名函数、读取excel、数据库操作、MD5加密、操作迁移redis

本周新学内容:

内置函数:

sorted()#排序
reversed() #反转
exec() #
eval()
all([ ])
any([ ])
dir()
zip()
a = [1,2,3]
b = [4,5,6]
for l1,l2 in zip(a,b):
pass

def func(name):
return name
map(func,[1,2,3,4,5])

def func2(num):
if num>5:
return True
filter(func2,[1,2,3,4,5,6,7,8])


round(3.11111,2)
max()
min()

lambda匿名函数:

def func():
num = int(input('num:'))
if num % 2 ==0:
print('是偶数')
return
else:
func()

f = lambda x,b:str(x+b)

函数递归
#递归,就是函数自己调用自己

excel的读取:

import xlrd #引用第三方模块

读:
book = xlrd.open_workbook(r'/Users/nhy/Desktop/中奖名单.xlsx')
sheet = book.sheet_by_index(0)
# sheet = book.sheet_by_name('sheet1')
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) #总共多少列


写:

import xlwt
book = xlwt.Workbook()
sheet = book.add_sheet('sheet1')
# sheet.write(0,0,'名字')
# sheet.write(1,0,'王庆柱')
# sheet.write(2,0,'王宇建')



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 field in stu:#控制列的
# sheet.write(row,col,field)
# col+=1 #
# row+=1

# for row,stu in enumerate(stus):#控制行
# for col,field in enumerate(stu):#控制列的
# sheet.write(row,col,field)
# book.save("students.xls")



操作mysql:
import pymysql
ip ="xxxx"
user = 'xxx'
password="xxx"
db='xxx'
port=3306
charset='utf8'
conn = pymysql.connect(host=ip,user=user,password=password,
db=db,port=port,charset=charset,autocommit=True)#建立连接

cur = conn.cursor(pymysql.cursors.DictCursor) #游标

sql = 'select * from app_myuser limit 5;'

cur.execute(sql)#执行sql语句,insert 、update 、delete
#conn.commit() #提交
all = cur.fetchall()
# one = cur.fetchone()
# many = cur.fetchmany(2)
cur.close()
conn.close()

# print(one)
# print(many)
print(all)

 读取redis:

import redis

#关系型数据库
#mysql oracle sqlserver、sqlite


#非关系型数据库 NoSQL
# mongodb
# redis 每秒钟达到10w次的读写,存在内存里面
r = redis.Redis(host='118.24.3.40',password='HK139bc&*',port=6379,db=0,decode_responses=True)

r2 = redis.Redis(host='118.24.3.40',password='HK139bc&*',port=6378,db=0,decode_responses=True)

#字符串
# r.set('mjz_students','{"msg":"sdfsdfsd"}')
# info = r.get('mjz_students')
# r.expire('dabaobao',30)#设置失效时间
# r.delete('mjz_students')
# r.set('mjz_students','{"msg":"sdfsdfsd"}')

#哈希类型

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的类型

#1、从aredis里面获取所有的key,
#2、判断key的类型
#3、根据key的类型,使用set /hset
#4、set到bredis里面


迁移redis:

import time

import redis
r = redis.Redis(host='118.24.3.40',password='HK139bc&*',port=6379,db=0,decode_responses=True)

#r2 = redis.Redis(host='118.24.3.40',password='HK139bc&*',port=6378,db=0,decode_responses=True)
#1、从aredis里面获取所有的key,
#2、判断key的类型
#3、根据key的类型,使用set /hset
#4、set到bredis里面

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

#管道

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)

 

上一篇:day5


下一篇:SQL练习-SQLZOO SQLZOO:More JOIN operations