分离封装的可复用的代码块
内置函数#python自带的,可直接使用
input()
print()
eval()
open(file,模式)#打开文件并返回文件对象,模式可选,默认只读’r’,‘w’覆盖写模式,‘a’追加写模式,encoding字符编码一般utf8
opfile=open(‘C:\Users\W^2\Desktop\666.txt’,‘r’,encoding=‘utf8’)
for filestr in opfile:
print(filestr)
opfile.close()
写入文件
opfile=open(‘C:\Users\W^2\Desktop\666.txt’,‘w’,encoding=‘utf8’)
opfile.write(‘ssss’)
opfile=open(‘C:\Users\W^2\Desktop\666.txt’,‘r’,encoding=‘utf8’)
for filestr in opfile:
print(filestr)
opfile.close()
标准库函数#python安装时也安装了标准库需要import
random()
sys,os,time
import time
curtime=time.time()
localtime=time.localtime(curtime)
loctimeasc=time.asctime(localtime)#带英文版的年月日的时间
strtime=time.strftime(’%Y-%m-%d %H:%M:%S’,localtime) #年月日时分秒格式化时间
import calendar
print(calendar.month(2023,1))#获取某年某月的日历import os
print(file)#获取当前文件名带路径的
print(os.path.abspath(file))#此处()中为__file__获取当前文件的绝对路径
print(os.path.dirname(os.path.abspath(file)))#获取当前文件的目录
path_split=os.path.split(file)#拆分文件和目录
join_path=os.path.join(‘path_split’,‘xxx.text’)
path_split=os.path.split(file)#拆分文件和目录,返回类型为元祖
print(path_split)
y=‘baseurl’
for x in path_split:
if not x.endswith(‘xlsx’):#判断字符串是否以xlsx结尾,如果不是,这个值就是文件的目录,否则就是文件名
y=x
break
data3=os.path.join(y,‘333.xlsx’)#生成新的文件目录
import random
print(random.random())#生成0-1的随机数
print(random.randint(1,5))#生成1-5的随机整数 第三方函数#需要下载再导入使用
数据库链接pymysql;读取excel:pandas,xlrd,openpyxl
下载三方函数

选中自己的项目
点击加号

输入自己要安装的三方库,安装

#连接数据库:pymysql
1.导包
import pymysql
2.创建连接
cnotallow=pymysql.connect(host=‘ip’,user=‘username’,passwd=‘password’,port=port,db=‘shujuku’)
3.获取游标操作数据库
cursor=conn.cursor()
4.准备sql语句
sql=“select * from enmp”
5.执行sql语句
cursor.execute(sql)
6.获取结果
result=cursor.fetchall()#返回的是二维元祖
result1=cursor.fetchone()#返回的是元祖类型
7.关闭连接
conn.close()
##通过配置文件链接数据库
###准备配置文件
db_config.ini内容为:
[db01]
host=127.0.0.1
user=root
password=20112441
port=3306
db=mysql####导包
import pymysql
import configparser
####获取数据库配置文件地址
path=“C:\Users\W^2\PycharmProjects\pythonProject2\testcase\pac\db_config.ini”
####要连接的数据库相当于项目名
switch_to=‘db01’
####读取配置文件
parser=configparser.ConfigParser()#配置文件解析器
parser.read(path,encoding=‘utf-8’)
host=parser.get(switch_to,‘host’)
user=parser.get(switch_to,‘user’)
passwd=parser.get(switch_to,‘password’)
port=int(parser.get(switch_to,‘port’))#端口为整数类型,需要进行转换
db=parser.get(switch_to,‘db’)
#上面代码也可改成
parser=configparser.ConfigParser()#配置文件解析器
parser.read(path,encoding=‘utf-8’)
peizhi=parser.items(switch_to)#将数据库配置信息存放在items中,一行为一个item
lianjie=dict(peizhi)#将含有数据库配置信息的items转化为字典
conn=pymysql.connect(**lianjie)#**是为了解包字典
#获取链接
conn=pymysql.connect(host=host,user=user,passwd=passwd,port=port,db=db)
#获取游标
cursor=conn.cursor()
#准备sql
sql=‘select * from xsb’
#执行sql
cursor.execute(sql)
#获取结果
re=cursor.fetchall()
#打印结果
for x in re:
print(x)
#如果是插入提交或者修改数据库语句还需要进行commit
#conn.commit()
#关闭链接
conn.close()#读取excel文件pandas,xlrd,openpyxl
准备excel文件(此处我的excel文件有两个工作簿)


##读取excel文件pandas
import pandas#导包
datapath=‘C:\Users\W^2\PycharmProjects\pythonProject2\testcase\pac\666.xlsx’#文件地址
data=pandas.read_excel(datapath)#读取文件,默认读取第一张表单
#或者读取某些列
#data1=pandas.read_excel(datapath,usecols=[‘列名1’,‘列名2’])
print(data1[‘列名1’]+‘-’+data1[‘列名2’])
data2=pandas.read_excel(datapath,sheet_name=1)#读取第二张表单,1相当于索引
print(data2,type(data2))#data2类型<class ‘pandas.core.frame.DataFrame’>
llist=data2.values.tolist()#转成列表后可进行我们熟悉的操作 可能会报错
如下图,原因是文件问题,可在pycharm外编辑文件后复制到项目里

##自定义函数#自己编写的
格式:
###定义函数
def 函数名(形参列表):#形参列表可以有0或多个参数,用应为,分割
代码块
return 返回值(若无返回值可不写这一句)
###调用函数
函数名(实参列表)#实参列表要与形参列表一致(无返回值)
变量名=函数名(实参列表)#实参列表要与形参列表一致(有返回值)
###带默认值的函数,
def 函数名(形参=默认值):#形参列表可以有0或多个参数,用应为,分割
代码块
return 返回值(若无返回值可不写这一句)
例:
def showme(name=‘张三’):
print(name)
showme()
showme(‘李四’)
def showyou(name,age=10):
print(name,age)
showyou(10)#打印10,10
showyou(22,33)#打印22,33
#装饰器(重要) 可以再不改变函数代码和调用方式下,为函数增加新功能
使用场景:
日志记录,权限检验,性能测试,事务处理
定义:
def 装饰器名(func):#func相当于接收被加强的函数的函数名,习惯写func写别的也可以,也可以写*func,代表任意参数,任意多个参数
def 内部函数名(形参列表):#形参列表相当于被加强的函数的参数列表
代码体
func(形参列表)#相当于自定义函数名(形参列表)
return 内部函数名 #固定格式要有这一句
调用:
@装饰器名
自定义函数
调用自定义函数#此时自定义函数已经有了装饰器包含的功能
例:定义
def zhuanshiqi(func):
def inner():
print(‘装饰器’)
print(f’被调用的函数名为{func.name}')#可打印被加强的函数名
func()
return inner 例:调用
@zhuanshiqi
def show():
print(‘自定义的函数’)
show()#调用被装饰器加强过的函数
3上


























