day8内置模块、导入模块、写日志、发邮件、操作redis

1.批量将数据写入excel

import xlwt
"""
批量将数据写入excel表格
"""
book=xlwt.Workbook()
sheet=book.add_sheet("xuesheng")

all_stu=[
["id","name","sex","phone","contry"],
["1","xiaohei","男","2313","china"],
["2","xiaohei","男","2313","china"],
["3","xiaohei","男","2313","china"],
["4","xiaohei","男","2313","china"],
["5","xiaohei","男","2313","china"],
["6","xiaohei","男","2313","china"],
]

#第一种方法
# line=0 #控制写入的行
# for stu in all_stu:
# col=0 #每次读取新的一行时列数初始化为0
# for data in stu:
# sheet.write(line,col,data)
# col+=1
# line+=1
# book.save("stu.xls")

#第二种方式使用内置函数enumerate
# lis={}
for line ,stu in enumerate(all_stu):
for col,data in enumerate(stu):
sheet.write(line,col,data)
book.save("stu1.xls")


2.python内置函数
# print(max([1,23,4,1,3,]))  #max()只要可以循环就行,int类型的数据
# print(min([1,23,4,1,3,]))
# print(round(1.1321,2)) #取几位小数
#sorted
# s="231232138"
# print(sorted(s)) #升序
# print(sorted(s,reverse=True)) #降序

# print(ord("a")) #将字符转换成对应的阿斯科码值
# print(chr(97)) #将数字转换成字母
# my_code="""
# def my():
# print("hello")
# my()
# """
# exec(my_code) #用来执行python代码,但是这个方法不安全
l=[1,2,4,11,55,]
def bl(i):
return (str(i).zfill(2))
# res=list(map(bl,l)) #map(func,list)这个方法就是传函数名,循环调用l中的值作为参数的值。map返回的值放在一个生成器中
m1=list(filter(bl,l)) #过滤返回值,如果返回的值是真,那就保留,如果是假不保留
print(m1)

3.发送邮件

import yagmail

user="134xxx@163.com"
password="xxxx" #这个要使用授权码,163邮箱的自己设置的授权码

m=yagmail.SMTP(host="smtp.163.com",user=user,password=password)
#如果是qq邮箱host=smtp.qq.com,qq邮箱必须添加smtp_ssl=True
m.send(to=["117xxxx@qq.com",],cc="511402xxx@qq.com",subject="好好上课",contents="上课",
attachments="内置函数.py")
#如果发送多个人to可以用list
#发送的附件的名字是乱码,需要使用上课用的yagmail新模块

4.写日志
import nnlog

log=nnlog.Logger("my.log",level='error', backCount=3,when="S")
#默认的日志级别是debug级别
#when控制生成日志的频率,D表示每天都生成日志,S表示每秒生成一个日志
#backCount默认备份日志的个数
log.info("预警")
log.error("出错")

5.操作redis

import  redis

ip='127.0.0.0'
port="6379"
password='HK139bc&*'

#decode_responses=True 设置取出来的数据都是string类型的
conn=redis.Redis(host=ip,port=port,password=password,db=2,decode_responses=True)

# res=conn.get("nn1") #获取使用get,会以二进制的byte形式显示,以b开头
# # conn.flushdb() #删除库所有数据
#
# conn.set("nn2","hahha",60) #修改和增加使用set方法,在后面设置失效时间,单位是s

# conn.set("python:os","list,set,map") #以文件夹的方式保存在redis数据库中
#
# res=conn.get("python:os")
# print(res.decode()) #byte数据类型转换成字符串使用decode()方法,字符串转换成byte使用encode()方法

#以Hash数据类型保存数据及操作数据

conn.hset("jnz_stu","nn2",{"sex":"男","age":"19"}) #添加键值对

# res=conn.hget("jnz_stu",'nn2') #获取数据

# conn.hdel("jnz_stu",'nn2') #删除数据

res=conn.hgetall("jnz_stu") #获取所有的值

# newdic={}
# # for key,value in enumerate(res):
# # newdic[key.decode()]=value.decode()
# # print(res)
# #第一种方法
# for key ,value in res.items():
# # print(key,value)
# newdic[key.decode()]=value.decode()

print(res)

 

posted on 2018-10-15 23:10  zz测试笔记  阅读(184)  评论(0编辑  收藏  举报