Python学习笔记(五)

第三方模块

  python有很多的第三方模块,安装后直接用就可以了,常用的三种安装方法如下:

  1、pip安装

    在计算机命令行模式下,运行 pip install pymysql

    如果安装不成功,可以FQ或者把pip源改为国内网站

  2、下载.whl文件

    在计算机命令行模式下,运行 pip install C:\Users\Desktop\pymysql.whl

  3、下载压缩包文件

    1.解压缩

    2.文件地址栏cmd 然后运行 python  setup.py install

 redis

  redis是非关系型数据库,以key - value形式存储,数据存在内存中,读取速度快

import redis
r = redis.Redis(host='1.1.1.1',port=6379,password='********&*',db=10) # 连接redis
# 操作string类型的
#
r.set('hahhah','范德萨发手动发的',50) # 增加数据,设置失效时间,默认-1永久生效
#
res = r.get('hahhah') # bytes 二进制
print(res)
print(res.decode()) # 编码,变成字符串
# 修改
r.set('hahhah','范德萨发手动发的')
# 删除
r.delete('hahhah') # 删除不存在的,不报错
# 其他的方法
print(r.keys()) # 获取到所有的key
print(r.keys('*info')) # 过滤,模糊匹配
print(r.exists('hahhah')) # 判断key是否存在
r.flushdb() # 清空当前数据库里所有的key
# 哈希类型 hash,字典嵌套字典
# 增删改查
r.hset('hhhaa','aaa','kjlkjlkjljkjk') # 增加,修改
r.hset('hhhaa','sss','fdsfsdfdsfdsf')
print(r.hget('hhhaa','aaa')) # 获取制定小key里面的数据
print(r.hgetall('hhhaa')) # 获取大key里面的数据
res = r.hgetall('hhhaa')
a = {}
for k,v in res.items():
    a[k.decode()] = v.decode()
print(a)
for i in res:
    res[i.decode()] = res.pop(i).decode()
print(res)
r.hdel('hhhaa','aaa') # 删除制定的小key
r.delete('hhhaa') # 直接删除大key
# 其他类型
r.expire('hhhaa',50) # 制定key的失效时间
print(r.ttl('hhhaa')) # 查看key的失效时间
print(r.type('hhhaa')) # 查看key的类型

pymysql

import pymysql
# 1.连上数据库,ip,账号密码,端口号,数据库
# 2.执行sql,获取结果
coont = pymysql.connect(host='1.1.1.1',user='xxxx',password='******',
                port=3306,db='xxx',charset='utf8',autocommit=True)
cur = coont.cursor() # 建立游标
res = cur.execute('insert into nhy VALUES ("","haha","123456");') # 执行sql语句,不会返回数据
sql = 'insert into nhy (NAME ,pwd) VALUES ("haha1","123456");'
res = cur.execute(sql)
coont.commit() # 提交
cur.execute('select * from nhy where name = "haha1"')
print(cur.fetchall()) # 获取查询到的所有结果,游标走到最后
print(cur.fetchone()) # 只获取一条
print(cur.fetchmany(11)) # 指定获取几条
cur.close() # 游标关闭
coont.close() # 连接关闭

nnlog

import nnlog
my_log = nnlog.Logger('aaa.log',level='debug',when='d',backCount=5)# 文件、级别、多久执行一次,保留几个文件
my_log.debug('这是debug的')
my_log.info('这是info的')
my_log.warning('这是waring的')
my_log.error('这是error的')

加密模块

import hashlib
s = '123456'
print(s.encode())
m = hashlib.md5(s.encode()) # md5加密算法不可逆
print(m.hexdigest()) # 获取加密后的结果,传byes类型的
# 所有一样的字符串,md5之后的结果都是一样的
# 撞库

发送邮件

import yagmail
# 账号、密码、邮箱服务器、收件人、抄送人、主题、正文、附件
# qq邮箱需要加smtp_ssl=True,安全协议
username = '[email protected]'
password = '***********' # 授权码
mail = yagmail.SMTP(user=username,password=password,host='smtp.163.com') # 连上邮箱
mail.send(to=['[email protected]','[email protected]'],
          cc='[email protected]',
          subject='邮件主题',
          contents='正文',
          attachments=r'C:\Users\Administrator\Desktop\啊啊啊啊.txt')
# 发送附件,中文是乱码,yagmail里面代码有问题,使用别的yamail

操作excel

import xlwt
book = xlwt.Workbook() # 创建excel
sheet = book.add_sheet('stu_info') # 创建sheet页
sheet.write(0,0,'学生编号') # 行、列、写什么
sheet.write(0,1,'学生姓名')
sheet.write(0,2,'学生成绩')
sheet.write(1,0,'1')
sheet.write(1,1,'聂磊')
sheet.write(1,2,'88.88')
book.save('stu.xls') # 用xls结尾,用xlsx的话office可能打不开,wps可以

猜你喜欢

转载自www.cnblogs.com/qingmingzy/p/9296118.html
今日推荐