文章目录
ini文件
ini文件编写格式
[节点]
选项=选项值
ps:节点不可以重复;注释是前面加分号;
ini文件读取
ini文件:
[database]
username=xxx
passwd=1111
import configparser
config=configparser.ConfigParser()#实例化
config.read("config.ini",encoding="utf‐8")#读取文件
sections=config.sections()#读取ini文件所有的节点
options=config.options(section="database")#获取具体节点的所有选项
value=config.get(section="database",option="username")#获取某个选项下的选项值
values=config.items(section="database")#获取某个节点下的所有选项和选项值
ini文件编辑
写入一个节点
new_section="userinfo1"
if new_section not in sections:
config.add_section("userinfo1")
# 给某个节点添加选项及选项值
config.set(section="userinfo1",option="username",value="hefan")
config.set(section="userinfo1",option="passwd",value="hefan")
with open("config.ini","w+") as file:
config.write(file)
删除节点
del_section="userinfo1"
print(sections)
if del_section in sections:
config.remove_section(section=del_section)
with open("config.ini","w+") as file:
config.write(file)
# 删除选项及选项值
config.remove_option(section="userinfo",option="passwd")
with open("config.ini","w+") as file:
config.write(file)
yaml文件
什么是yaml文件
YAML 是一种灵活的数据格式,支持注释、换行符、多行字符串、裸字符等
在自动化过程中,我们很多地方都需要使用配置文件来储存数据
比如测试环 境,数据库信息、账号信息、日志格式、日志报告名称等。
其中,yaml文件是最常用的配置文件类型之一,相比较ini,conf配置文件
来说,它更加简洁,操作更加简单,同时还可以存放不同类型的数据。
后缀名:.yaml .yml
yaml文件支持那些数据类型
对象、数组、纯量、字符串、数值、bool
yaml文件编写语法规则
- 大小写敏感
- 使用缩进表示层级关系
- 缩进用空格,相同的层级元素左对齐即可
- #:表示注释
yaml文件的读取
反序列化:从文件转换为python对象
import yaml
with open("xxx.yaml","r") as file:
data=yaml.load(stream=file,Loader=yaml.FullLoader()
print(data)
yaml文件写入数据
纯量、对象、数组(Python对象) -----》yaml文件 序列化(持久化)
modules=["中文","pytest","unittest","requests","requests"]
with open("modules.yaml","w+") as file:
yaml.dump(data=modules,stream=file,allow_unicode=True,encoding="utf‐8")
excel文件
第三方库:openpyxl
openpyxl模块
三大组件:
- 工作簿:包含多个sheet
- 工作表
- 单元格
import openpyxl
"""EXCEL的处理:
1、创建excel文件
2、读取excel
"""
def createExcel():
#创建工作簿
wk=openpyxl.Workbook()
#获取当前工作表
sheet=wk.active
#写数据到单元格
sheet.cell(1,1).value="username"
sheet.cell(1,2).value="class"
wk.save("xxx.xlsx")
def readExcel(filepath):
#读取excel文件
wk=openpyxl.loda_workbook(filepath)
#获取工作表方式一
sheet=wk.get_sheet_by_name("Sheet")
#获取工作表方式二
sheet=wk["Sheet"]
#获取单元格坐标
location=(sheet.cell(1,1))
#获取单元格值
value=sheet.cell(1,1).value
#获取工作表行数和列数
rows=sheet.max_row
cols=sheet.max_column
datalist=[]
for row in range(1,rows+1):
for col in range(1,cols+1):
value=sheet.cell(row,col).value
datalist.append(value)
return datalist
def printDataExcel(filepath):
data=readExcel(filepath)
print(data)
def editExcel(filepath):
wk=openpyxl.load_workbook(filepath)
#创建新的工作表
mysheet=wk.create_sheet("mysheet")
mysheet.cell(1,1).value="username"
wk.save(filepath)
日志收集
内置模块logging–四大组件
- 日志器Logger—>入口
- 处理器Handler—>执行者,决定日志在不同端进行输出(日志文件、控制台)
- 格式刷Formatter—>日志输入的内容
- 过滤器Filter---->输出感兴趣日志信息,过滤掉不需要的
关系:1个日志器可以有多个处理器,每个处理器都可以有各自的格式器和过滤器
logging模块的应用
从低到高日志级别:
debug 调试信息
info 关键时间调试
waring 警告信息
error 错误信息
critical 严重错误信息
#创建日志器
logger=logging.getLogger("logger")
# 日志输出当前级别及以上级别的信息,默认日志输出最低级别是warning
if not logger.handlers:
logger.setLevel(logging.INFO)
# 创建控制台处理器‐‐‐‐》输出控制台
SH = logging.StreamHandler()
# 创建文件处理器‐‐‐‐》输出文件
FH = logging.FileHandler("log.txt")
# 日志包含哪些内容 时间 文件 日志级别 :事件描述/问题描述
formatter = logging.Formatter(fmt="[%(asctime)s] [%(filename)s] %(leveln
ame)s :%(message)s",
datefmt='%Y/%m/%d %H:%M:%S')
logger.addHandler(SH)
logger.addHandler(FH)
SH.setFormatter(formatter)
FH.setFormatter(formatter)
try:
score = int(input("请你输入你的成绩"))
if score > 60:
print("恭喜你通过")
else:
print("继续努力")
logging.debug("这是一个debug信息")
logger.info("你查询成绩成功")
logger.warning("这是一个警告信息")
except Exception as error:
logger.error("输入不是数字,错误信息:" + str(error))
logger.critical("这是一个critical信息")
操作mysql
第三方库:mysql-connector、pymysql
mysql-connector操作mysql步骤
- 创建连接
- 创建游标实例
- 调用execute(参数1,参数2,参数3)
参数1:sql语句
参数2:类型为远足,元素值为sql语句占位符对应的参数
参数3:参数bool类型,第一个蚕食是不是多sql语句,如果是则传入True,否则传入
False
import mysql.connector
def connect():
conn=mysql.connector.connect(
host="localhost",
user="root",
password="admin")
return conn
#连接某个数据库
def connect(database):
conn=mysql.connector.connect(
host="localhost",
user="root",
password="admin",
database=database
)
return conn
#创建数据库、创建表
def createdatabase(databasename):
conn=mysql.connector.connect(
host="localhost",
user="root",
password="admin"
)
#获取游标实例
cursor=conn.cursor()
sql=f"create database {databasename}"
cursor.excute(sql)
cursor.excute("show databases")
for database in cursor:
print(database)
#创建表
def createtable():
1068

被折叠的 条评论
为什么被折叠?



