一、xlwt

xlwt是python第三方模块,主要是对excel的写操作。xlwt使用时必须先安装。

1、安装

在操作系统的cmd窗口输入pip install xlwt回车即可在线安装。

安装完成后,在写python使用时,需要引入 import xlwt

2、写excel

book = xlwt.Workbook() #新建一个excel
sheet = book.add_sheet('sheet1')#加sheet页
sheet.write(0,0,'姓名')#行、列、写入的内容
sheet.write(0,1,'年龄')
sheet.write(0,2,'性别')
book.save('user.xls')#结尾一定要用.xls

详细用法可参考

https://blog.csdn.net/Tulaimes/article/details/71172778

https://www.cnblogs.com/linyfeng/p/7123423.html

二、mysql连接

mysql的使用要安装pymysql模块。安装方法同样在cmd窗口输入pip install pymysql.使用的时候要引入import pymysql

mysql连接分几个步骤:

1、连接上数据库

2、创建游标、

3、执行sql

4、获取结果

5、关闭游标

6、关闭连接

示例:select语句

import pymysql
coon = pymysql.connect(
host='localhost',user='root',passwd='',
port=3306,db='shujuku',charset='utf8'
#port必须写int类型,
#charset这里必须写utf8
)
cur = coon.cursor() #建立游标
cur.execute('select * from user;')#执行sql语句
res = cur.fetchall() #获取所有返回的结果,返回类型是个嵌套元祖
cur.close()#关闭游标
coon.close()#关闭连接

select语句的使用如上。但是update\insert\delete语句操作有些不同,因为他们修改了数据库,所以在操作之后必须commit下

示例:

coon = pymysql.connect(
host='localhost',user='root',passwd='', #host根据要连接数据库的实际ip填写。
port=3306,db='shujuku',charset='utf8'
#port必须写int类型,
#charset这里必须写utf8
)
cur = coon.cursor() #建立游标
cur.execute('insert into user (id,name,passwd) VALUE (1,"aa","123456");')
#如果插入的数据值变量存储的话,可以这么写:cur.execute("insert into user(id,name,passwd' values ('%s','%s','%s');"%(id,username,passwd))
#其中%(id,username,passwd)必须写在双引号外边。如果写在里边就成了一个字符串了
)
coon.commit() # delete update insert都必须得coomit
cur.close()#关闭游标
coon.close()#关闭连接

mysql用游标默认用select查出来的表数据,返回的格式是两层元祖。如果想要对数据进行再次操作,就要对这个2层元组操作(('1','小红','123456'),('2','小明','123456'))

示例:将mysql数据库读出来的数据写入excel中

import pymysql,xlwt
def sql_to_excel(res):
# print(res)
book=xlwt.Workbook()
sheet=book.add_sheet('stu')
sheet.write(0, 0, '编号') # 行、列、写入的内容
sheet.write(0, 1, '姓名')
sheet.write(0, 2, '性别')
j=1#行
for tuple11 in res:#获取到内层元组
i = 0 # 列,每次都从第0列开始写
for t in tuple11:#从内层元组中获取元素,并循环写入每个元素到对应的行,列
sheet.write(j,i,t)
i=i+1
j=j+1
book.save('user.xls')
conn=pymysql.connect(host='localhost',user='root',passwd='',
port=3306,db='shujuku',charset='utf8')
cur=conn.cursor()
cur.execute('select * from user;')
res=cur.fetchall()
sql_to_excel(res)
cur.close()
conn.close()

如果想要从数据库查出来的数据返回格式为字典类型,则需要设置一下游标cursor的类型

cur = coon.cursor(cursor=pymysql.cursors.DictCursor) #建立游标,指定cursor类型返回的是字典。

cursor也可以对取出数据的条数进行限制

cur.fetchall()#获取查询结果的所有数据

cur.fetone()#获取查询结果的第一条数据

cur.fetmany(n)获取查询结果的n条数据

举例:

import pymysql
def my_db(sql,port=3306,charset='utf8'):
import pymysql
host, user, passwd, db = 'localhost','root','','stu' coon = pymysql.connect(user=user,host=host,port=port,passwd=passwd,db=db,charset=charset)
cur = coon.cursor(cursor=pymysql.cursors.DictCursor) #建立游标,指定cursor类型返回的是字典
cur.execute(sql)#执行sql
if sql.strip()[:6].upper()=='SELECT':
# res = cur.fetchall()#返回select语句查询结果的所有数据
# fileds = []
# for filed in cur.description:#cur.description获得的是表结构,二位元祖,可用此方法得到字段名:(('id', 3, None, 11, 11, 0, True), ('name', 253, None, 20, 20, 0, True)) ('sex', 253, None, 20, 20, 0, True))
# fileds.append(filed[0])
fileds = [ filed[0] for filed in cur.description ] #和上面3行代码的意思是一样
print(fileds) # cur.fetchmany(n) #能传入一个数,返回多少条数据 res= 'xx'
else:
coon.commit()
res = 'ok'
cur.close()
coon.close()
return res
res = my_db('select * from users_info limit 10;')
print(res)

三、hashlib

#加密,常见的加密方法md5,生成结果是固定的128 bit字节,通常用一个32位的16进制字符串表示.md5加密是不可解得

hashlib是内置模块,不需要安装,在使用时只需要引入import hashlib

import hashlib
m = hashlib.md5()
passwd = ''
m.update(passwd.encode()) #不能直接对字符串加密,要先把字符串转成bytes类型
print(m.hexdigest())

还有hashlib.sha512,hashlib.sha256等,加密后的字符串更长,更安全,但是相对来说也比较慢

最新文章

  1. andriod刷机
  2. Javascript初学篇章_1(概念/数据类型)
  3. Hanio汉诺塔代码递归实现
  4. 一个可以设置所有子控件是否可以点击的Layout的实现
  5. Linux内核OOM机制的详细分析(转)
  6. linux中的文件属性
  7. 通过示波器分析TypeB卡通讯数据
  8. Python3 如何优雅地使用正则表达式(详解六)
  9. hdu 1698 Just a Hook_线段树
  10. Inno setup complier将文件添加注册表
  11. Nexus搭建私服 学习
  12. iOS多线程开发之GCD(中篇)
  13. mysql常用基础操作语法(八)~~多表查询合并结果和内连接查询【命令行模式】
  14. java中的异常(一)
  15. hdu4942线段树模拟rotate操作+中序遍历 回头再做
  16. angular 项目 error TS2451: Cannot redeclare block-scoped variable 'ngDevMode'
  17. phpStorm中使用xdebug断点调试php代码
  18. 8 并发编程-(线程)-多线程与多进程的区别&Thread对象的其他属性或方法
  19. WebGL学习笔记(二)
  20. bzoj 3834 [Poi2014]Solar Panels 数论分块

热门文章

  1. Java 字符流
  2. find the lowest number location
  3. C#LeetCode刷题-树
  4. C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)
  5. Linux-Centos 用crontab定时运行python脚本详细步骤
  6. C#图解教程(第四版)—02—类的基本概念
  7. vue cli3.0使用svg全过程
  8. 微信小程序自动化测试最佳实践(附 Python 源码)
  9. Java反射概念与基础
  10. ucore lab2