python(五)模块
列表生成式
需求:要产生1-10的随机数字,不足两位的前面补0,注意01,02,03......前面补了0就不是int类型所以要转换为字符串,1,2,3.....这样的才属于int类型
正常的写法:
f = [ ]
for i in range(1,11):
f.append(str(i).zfill(2))#因为要产生两个数字,不足补0,01,02不是int类型是字符串
print(f)
打印结果:
['01', '02', '03', '04', '05', '06', '07', '08', '09', '10']
列表生成式的写法:
f1 =[str(i).zfill(2) for i in range(1,11)]
print(f1)
打印结果:
['01', '02', '03', '04', '05', '06', '07', '08', '09', '10']
用列表生成式写出创建10个文件及10个文件夹:
#创建10个txt文件
f1 =[open('%s.txt'%i,'w') for i in range(10)]
#创建10个文件夹
import os
f2= [os.mkdir(str(i)) for i in range(10)]#文件夹名字类型不能是数字需先转为字符串
三元表达式
三元表达式的写法:
sex_tag = 0
if sex_tag == 0:
sex='女'
else:
sex='男'
#上面的判断写成三元表达式:
sex ='女' if sex_tag == 0 else sex='男' #if满足取前面否则取后面
排序写法:
#升序的排列
s='1283225'
f=['42','2','1']
print(sorted(f))
print(sorted(s))
打印结果:
['1', '2', '42']
['1', '2', '2', '2', '3', '5', '8']
#降序排列
s='1283225'
f=['42','2','1']
print(sorted(f,reverse=True))
print(sorted(s,reverse=True))
打印结果:
['42', '2', '1']
['8', '5', '3', '2', '2', '2', '1']
python的os模块补充
#getcwd获取当前路径
import os
print(os.getcwd())#获取当前的路径
#进入到某个路径下创建文件
import os
os.chdir(r'D:\untitled1\day6模块')#进入某个文件夹
print(os.getcwd())#获取当前路径
open('dm.txt','w')#创建一个文件
#os.system及os.popen都是执行操作系统命令,前者拿不到执行结果,后者可以拿到执行结果
import os
os.chdir(r'C:\Users\dm.liang\Desktop')
print(os.getcwd())
os.system('dir')#system是执行操作系统命令,dir看桌面上的文件
#os.popen
import os
os.chdir(r'C:\Users\dm.liang\Desktop')
os.system('ipconfig')#os.system执行操作系统命令,但是拿不到结果
result=os.popen('ipconfig').read()#os.popen执行操作系统命令,可以拿到结果
print(result)
import os
os.path.getatime()#括号内写上文件名,可以获取文件创建时间
os.path.getatime()#获取文件最后访问时间
os.path.getmtime()#获取文件最后修改时间
os.path.join()#拼接路径的
os.path.exists()#判断目录、文件是否存在的
os.path.split()#分隔文件路径和文件名
#看盘中中有多少.mp4的视频
import os
for cur_dir,dirs,files in os.walk(r'D:/'):
print(cur_dir)#当前循环到哪个目录了
print(dirs)#当前目录下的所有文件夹
for f in files:#当前目录下所有的文件
if f.endswith('.mp4'):
print('发现小电影%s,目录正在%s'%(f,cur_dir))
time模块
#时间戳:124125411 格式化的时间:2019-05-20 12:00:00
import time
result=time.time()#获取当前的时间戳
print(result)
res=time.strftime('%Y-%m-%d %X')#转换为当前格式化的时间
print(res)
#时间戳跟格式化时间转换需先存时间元组
import time
time_tuple = time.gmtime(124244545)#把时间戳转换成时间元组,时间戳跟格式化时间转换需先存时间元组
res=time.strftime('%Y-%m-%d %H:%M:%S',time_tuple)#转换为当前格式化的时间
print(res)
#取当地时区
import time
time_tuple = time.localtime(124244545)#取当地时间用localtime
res=time.strftime('%Y-%m-%d %H:%M:%S',time_tuple)#转换为当前格式化的时间
print(res)
python操作mysql
注意:查询的sql不需要commit,但是update,delete,insert语句需要commit
#数据库操作查询语句
import pymysql
#连接数据库,其中port是int类型,charset='utf8'
coon=pymysql.connect(host='118.24.3.40',port=3306,user='jxz',password='123456',db='jxz',charset='utf8')
cur = coon.cursor()#建立游标就相当于仓库管理员,让仓库管理员给你去拿数据
sql ='select * from app_myuser limit 5;'
cur.execute(sql)#execute只是执行sql语句不能返回结果
res=cur.fetchall()#获取所有的结果
print(res)
cur.close()#游标关闭
coon.close()#连接关闭
#数据库操作插入语句
import pymysql
#连接数据库,其中port是int类型,charset='utf8'
coon=pymysql.connect(host='118.24.3.40',port=3306,user='jxz',password='123456',db='jxz',charset='utf8')
cur = coon.cursor()#建立游标就相当于仓库管理员,让仓库管理员给你去拿数据
sql ="insert into app_myuser(username,passwd,is_admin) values ('ldm','99889988','819');"
cur.execute(sql)#execute只是执行sql语句不能返回结果
coon.commit()#所有的update,insert,delete语句都要修改数据库数据需要commit下
res=cur.fetchall()#获取所有的结果
print(res)
cur.close()#游标关闭
coon.close()#连接关闭
注意:如果执行update,delete,insert语句不想写commit,可以直接在数据库连接后面加autocommit =True
import pymysql
#连接数据库,其中port是int类型,charset='utf8'
coon=pymysql.connect(host='118.24.3.40',
port=3306,user='jxz',
password='123456',db='jxz',
charset='utf8',
autocommit =True
)
cur = coon.cursor()#建立游标就相当于仓库管理员,让仓库管理员给你去拿数据
sql ="insert into app_myuser(username,passwd,is_admin) values ('ldm','99889988','819');"
cur.execute(sql)#execute只是执行sql语句不能返回结果
res=cur.fetchall()#获取所有的结果
print(res)
cur.close()#游标关闭
coon.close()#连接关闭
#fetchone与fetchall的区别是前者sql执行结果知道只能返回一条结果,后者是sql返回所有的结果
import pymysql
#连接数据库,其中port是int类型,charset='utf8'
coon=pymysql.connect(host='118.24.3.40',port=3306,user='jxz',password='123456',db='jxz',charset='utf8')
cur = coon.cursor()#建立游标就相当于仓库管理员,让仓库管理员给你去拿数据
sql ="select * from app_myuser where username='ldm';"
cur.execute(sql)#execute只是执行sql语句不能返回结果
res=cur.fetchone()#获取一条结果
print(res)
cur.close()#游标关闭
coon.close()#连接关闭
#如果想返回的结果展示字典形式,就在建立游标的时候加上pymysql.cursors.DictCursor
import pymysql
#连接数据库,其中port是int类型,charset='utf8'
coon=pymysql.connect(host='118.24.3.40',port=3306,user='jxz',password='123456',db='jxz',charset='utf8')
cur = coon.cursor(pymysql.cursors.DictCursor)#建立游标就相当于仓库管理员,让仓库管理员给你去拿数据
sql ="select * from app_myuser where username='ldm';"
cur.execute(sql)#execute只是执行sql语句不能返回结果
res=cur.fetchone()#获取所有的结果
print(res)
cur.close()#游标关闭
coon.close()#连接关闭
md5加密
md5加密用到hashlib模块,md5加密是不可逆的,加密后是转换不回来的
#获取md5加密后的结果
import hashlib
s='123456'
s.encode()#不能对字符串进行加密需要先转换为二进制
m =hashlib.md5(s.encode())#加密
res = m.hexdigest()#获取加密后结果
print(res)
写入excel
import xlwt
book=xlwt.Workbook()#新建一个excel
sheet=book.add_sheet('团建名额表')#新建一个sheet页,名字叫团建名额表
sheet.write(0,0,'姓名')#第一个0是行,第二个0是列,表示第一行第一列写上‘姓名’
sheet.write(0,1,'年龄')#第一行第二列写上年龄
book.save('tuanjian.xls')#保存
浙公网安备 33010602011771号