- 1.txt文件
- 2.yaml文件读取数据
- 3.openpyxl操作excel
- 4.csv文件 叫逗号分割值文件,也可以叫字符分割符文件
1.txt文件
# 1.文件的具体应用
# web自动化 接口自动化 读取文件里面的内容 测试数据 yaml文件,excel文件数据
# yaml文件怎么写数据 读数据
# 1.yaml文件读取数据
# 2.excel文件读取数据
# 3.txt文件读取
# 4.csv文件
f=open('./data/test.txt','r',encoding = 'utf-8')
print(f.read())
f.close()
with open('./data/test.txt','r',encoding = 'utf-8') as f:
print(f.read())
print(f.readline())
print(f.readlines())
2.yaml文件读取数据
# 创建了一个yaml文件
# 语法格式:
# 1.缩进表示层级关系 不能用tab按键,直接用空格
# 2.#表示注释
# 3.字符串不需要打引号,数字想要是字符串需要自己手动打引号
# 4.可以创建列表 [] -表示列表
# 5.可以创建字典{key:value}: 表示字典
# 6.yaml中的数据要空格
# 1.安装yaml
# 2.cmd输入pip install pyyaml 检测 pip list
# 3.setting里面安装
# 读取yaml文件里面的数据
import yaml
# 打开yaml
f=open('./data/dict1.yaml','r',encoding='utf-8')
# 读取加载yaml 5.1以后要用Loader=yaml.FullLoder
data=yaml.load(f,Loader=yaml.FullLoader)
print(data)
f.close()
with open('./data/dict1.yaml','r',encoding='utf-8') as f1:
data = yaml.load(f1,Loader = yaml.FullLoader)
print(data['person']['name'])
print(data['adress'])
# data是拿到所有数据 秋水 字典怎么取值 拿到键 就拿到值
#问题: 怎么拿到秋水这个值
#{'code':200,{'name':admin,'sex':'女'},adress:'changsha','token':'xxxxx'}
# 读取yaml中列表的数据
# 1.导入yaml
# 2.打开yaml文件
# 3.加载yaml文件数据
# 4.打印yaml文件内容
with open('./data/list_yaml.yaml','r',encoding='utf-8') as f:
data = yaml.load(f,Loader=yaml.FullLoader)
print(data)
#想要拿虚竹这个值,怎么拿,列表怎么取值 拿到小狗怎么拿
# print(data[1])
print(data[1][1])
# 列表嵌套字典
# [{'id':1,'name':'小翠','age':18},{'id':2,'name':'小花','age':19}
with open('./data/listdict_yaml.yaml','r',encoding='utf-8') as f:
data=yaml.load(f,Loader=yaml.FullLoader)
print(data)
3.openpyxl操作excel
# openpyxl只支持xlsx不支持xls,xls用xlrd,xlwt
# 读取excel数据 xlrd xlwt xlutils pandas openpyxl
#.1.安装 pip install openpyxl pip list
# 操作excel
# 读取excel数据
import openpyxl
# 打开excel
workbook = openpyxl.load_workbook('./data/bb.xlsx')
print(workbook)
# 拿表单 指定表单拿里面的数据
sheet = workbook['Sheet3']
print(sheet)
# 读取excel里面的数据 for
for i in sheet.values:
print(i)
# 拿到所有的表单数据
sheet=workbook.sheetnames
print(sheet)
# 拿到所有的表单数据
# 表单先都拿出来 循环列表
# 表单里面的值 workbook[Sheet1], workbook[Sheet2], workbook[Sheet3]
for i in sheet:
print(i)
for j in workbook[i].values:
print(j)
# 不要所有的数据 只要某一个值
# 指定表单 指定单元格的数据
sheet = workbook['Sheet3']
# 指定单元格的数据
id = sheet['A1'].value
print(id)
# # 指定单元格的数据 行和列
id = sheet.cell(row=1,column=1).value
print(id)
# 写入数据
# sheet.cell(row=1,column=5).value = '预期结果'
sheet['E1'].value= '预期结果3'
workbook.save('./data/bb.xlsx')
4.csv文件 叫逗号分割值文件,也可以叫字符分割符文件
# 内容,隔开
# 怎么操作csv文件
# 1.打开文件
# 2.读取 用的是csv.reader() 返回的是一个csv.reader的对象,可迭代的对象
# 我们对象进行遍历,输出每一行,每一列
import csv
f=open('./data/data.csv','r',encoding='utf-8')
a=csv.reader(f)
for i in a:
print(i)
with open('./data/data.csv','r',encoding='utf-8') as f:
a = csv.reader(f)
print(a)
# for i in a:
# print(i)
# print(i[1])
# 某一行的数据 转成
result = list(a)
print(result)
print(result[1])
# 写入writer
stu1 = [1,'秋水',18,'女']
stu2 = [2,'虚竹',108,'妖']
out = open('./data/data.csv','a',encoding='utf-8',newline='')
write=csv.writer(out)
write.writerow(stu1)
write.writerow(stu2)
data = [('测试1','测试2'),
('测试2','测试2')
('测试3','测试2')
('测试4','测试2')
('测试5','测试2')
]
f1= open('./data/data.csv','a',encoding='utf-8',newline='')
out=csv.writer(f1)
for i in data:
out.writerow(i)



