json文件处理
json 文件在项目中以配置文件使用场景居多。
data/data.json
{
"appPackage":"xxxxxxxx",
"appActivity":"xxxxxxxxx",
"noReset":true
}
读取json文件转换为python对象
使用json模块中的load方法;
testcases/test_data.py
import os
import json
root_dir = os.path.dirname( os.path.dirname(os.path.abspath(__file__)))
data_json = os.path.join(root_dir,'data/data.json')
def test_json_data():
"""
读取data.json 转换为Python对象
:return:
"""
with open(data_json,encoding='utf8') as file:
jsondata = json.load(file)
print(jsondata, type(jsondata))
将python对象保存到json文件中
使用json的 dump 方法可以将 Python的对象转换化json数据文件
import os
import json
root_dir = os.path.dirname( os.path.dirname(os.path.abspath(__file__)))
# 设置json文件路径
data_to_json = os.path.join(root_dir,'data/data2.json')
def test_json_save_data():
testdata = {
"platform":"Android",
"version":"7.0.1",
}
# 创建文件
with open(data_to_json,mode='w',encoding='utf8') as file:
# 将python对象的数据保存到文件中
json.dump(testdata,file)
yaml 文件处理
yaml 数据文件教程
http://www.ruanyifeng.com/blog/2016/07/yaml.html
python解析yaml文件
https://pypi.org/project/PyYAML
pyyaml 使用教程
https://pyyaml.org/wiki/PyYAMLDocumentation
安装
pip install pyyaml
解析yaml文件为Python对象
使用yaml中dump方法可以加载yaml文件转换为python对象
data/data.yaml
languages:
- Ruby
- Perl
- Python
websites:
YAML: yaml.org
Ruby: ruby-lang.org
Python: python.org
Perl: use.perl.org
testcases/test_data.py
import os
import json
import yaml
root_dir = os.path.dirname( os.path.dirname(os.path.abspath(__file__)))
data_yaml = os.path.join(root_dir,'data/data.yaml')
def test_yaml_python_data():
with open(data_yaml,encoding='utf8') as file:
data = yaml.load(file,Loader=yaml.SafeLoader)
print(type(data),data)
将python对象转换为yaml数据
使用 yaml中的load方法将字典数据转换到yaml文件中。
testcases/test_data.py
import os
import json
import yaml
root_dir = os.path.dirname( os.path.dirname(os.path.abspath(__file__)))
data_yaml = os.path.join(root_dir,'data/data.yaml')
yaml_file = os.path.join(root_dir,'data/data2.yaml')
def test_python_data_yaml():
testdata = {
"platform": "Android",
"version": "7.0.1",
}
with open(file=yaml_file,mode='w',encoding='utf8') as file:
yaml.dump(testdata,file)
excel文件处理
使用openpyxl 库进行 Excel文件的读写
参考手册: https://openpyxl.readthedocs.io/en/stable/
安装
pip install openpyxl
数据写入到Excel文件
import os
import json
import yaml
root_dir = os.path.dirname( os.path.dirname(os.path.abspath(__file__)))
data_yaml = os.path.join(root_dir,'data/data.yaml')
yaml_file = os.path.join(root_dir,'data/data2.yaml')
excel_file = os.path.join(root_dir,'data/data.xlsx')
from openpyxl import Workbook
def test_python_data_excel():
testdata = [
{"token": "xxxxxxxx"},
{"token": "d1563473-1f0d-4307-9774-6c2ff49c93ab"},
{"token": "d1563473-1f0d-4307-9774-6c2ff49c93a"}
]
# 创建 Excel 文件
wb = Workbook()
# 创建工作表
wc = wb.create_sheet("用户登录")
# 设置单元格的值
wc.cell(row=1,column=1,value='token')
wc.cell(row=2,column=1,value='d1563473-1f0d-4307-9774-6c2ff49c93ab')
wc.cell(row=3,column=1,value='d1563473-1f0d-4307-9774-6c2ff49c93a')
wc.cell(row=4,column=1,value='xxxxxxxx')
# 保存数据文件
wb.save(filename=excel_file)
思考: 如何通过读取 test_data中的数据保存到 Excel文件中。