Python模块(三)操作日志、邮件、Excel

模块的原理:就是把这个python文件执行了一遍

a.py  b.py

在b文件直接引用a文件

import a

 

查找模块的顺序

1.导入模块的时候首先从当前目录去找

2.如果当前目录下没有,就是去python的环境变量里面找

 

 日志相关模块操作

import nnlog

log = nnlog.Logger('test.log',level='error',backCount=5,when='S')
#默认debug级别;level打印日志的级别,该级别以下的不会打印;backCount保留几个日志;when按天(D)产生日志

log.debug('debug...')#一些调试信息,变量值等等
log.info('info...')#一些提示信息
log.warning('warning...')#出警告了
log.error('error...')#出错的时候打印

发邮件
import yagmail

user = '429255693@qq.com'
passwd = 'vgdyoripuptjcagc'#用授权密码
smtp_host = 'smtp.qq.com'
mail = yagmail.SMTP(user=user,password=passwd,host=smtp_host,smtp_ssl=True)#链接邮箱;腾讯邮箱需要加smtp_ssl=True,其他的不用
mail.send(to='437448252@qq.com',
cc='1607753696@qq.com',
subject = '测试报告',
content = '测试通过,可以上线',
attachments = ['E:\pycharm\脚本\day7\test.log'])#cc抄送;发送给多个人/多个文件用list['1234566@qq.com','437448252@qq.com']


创建Excel
import xlwt

book = xlwt.Workbook()#创建Excel
sheet = book.add_sheet('sheet名称')#添加sheet页
sheet.write(0,0,'姓名')#第一个0是行,第二个0是列
sheet.write(0,1,'成绩')
book.save('stu.xls')#保存Excel文件

读取Excel
import xlrd
book = xlrd.open_workbook('stu.xls')#打开Excel
sheet = book.sheet_by_index(0)#选择第几个sheet页
print(sheet.row_values(0))#某一行的数据
print(sheet.col_values(0))#某一列的数据
print(sheet.cell(0,0).value)#某个单元格的数据
print(sheet.nrows)#总共有多少行
print(sheet.ncols)#总共有多少列

for i in range(sheet.nrows):
print(sheet.row_values(i))#显示每一行的数据
修改Excel
import xlrd
from xlutils import copy

book = xlrd.open_workbook('stu.xls')
sheet0 = book.sheet_by_index(0)#获取原来的数据

new_book = copy.copy(book)
sheet = new_book.get_sheet(0)#新的sheet页
sheet.write(0,6,'年龄阶段')

for i in range(1,sheet0.nrows):
age = sheet.row_values(i)[3]
if age <= 18:
word = '未成年'
elif age > 18 and age <= 30:
word = '青年'
else:
word = '中年人'
sheet.write(i,6,word)

new_book.save('stu.xls')
 

posted @ 2019-05-20 19:26  辰毒秀  阅读(406)  评论(0编辑  收藏  举报