json文件处理

json 文件在项目中以配置文件使用场景居多。

data/data.json

  1. {
  2. "appPackage":"xxxxxxxx",
  3. "appActivity":"xxxxxxxxx",
  4. "noReset":true
  5. }

读取json文件转换为python对象

使用json模块中的load方法;
testcases/test_data.py

  1. import os
  2. import json
  3. root_dir = os.path.dirname( os.path.dirname(os.path.abspath(__file__)))
  4. data_json = os.path.join(root_dir,'data/data.json')
  5. def test_json_data():
  6. """
  7. 读取data.json 转换为Python对象
  8. :return:
  9. """
  10. with open(data_json,encoding='utf8') as file:
  11. jsondata = json.load(file)
  12. print(jsondata, type(jsondata))

将python对象保存到json文件中

使用json的 dump 方法可以将 Python的对象转换化json数据文件

  1. import os
  2. import json
  3. root_dir = os.path.dirname( os.path.dirname(os.path.abspath(__file__)))
  4. # 设置json文件路径
  5. data_to_json = os.path.join(root_dir,'data/data2.json')
  6. def test_json_save_data():
  7. testdata = {
  8. "platform":"Android",
  9. "version":"7.0.1",
  10. }
  11. # 创建文件
  12. with open(data_to_json,mode='w',encoding='utf8') as file:
  13. # 将python对象的数据保存到文件中
  14. 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

安装

  1. pip install pyyaml

解析yaml文件为Python对象

使用yaml中dump方法可以加载yaml文件转换为python对象
data/data.yaml

  1. languages:
  2. - Ruby
  3. - Perl
  4. - Python
  5. websites:
  6. YAML: yaml.org
  7. Ruby: ruby-lang.org
  8. Python: python.org
  9. Perl: use.perl.org

testcases/test_data.py

  1. import os
  2. import json
  3. import yaml
  4. root_dir = os.path.dirname( os.path.dirname(os.path.abspath(__file__)))
  5. data_yaml = os.path.join(root_dir,'data/data.yaml')
  6. def test_yaml_python_data():
  7. with open(data_yaml,encoding='utf8') as file:
  8. data = yaml.load(file,Loader=yaml.SafeLoader)
  9. print(type(data),data)

将python对象转换为yaml数据

使用 yaml中的load方法将字典数据转换到yaml文件中。
testcases/test_data.py

  1. import os
  2. import json
  3. import yaml
  4. root_dir = os.path.dirname( os.path.dirname(os.path.abspath(__file__)))
  5. data_yaml = os.path.join(root_dir,'data/data.yaml')
  6. yaml_file = os.path.join(root_dir,'data/data2.yaml')
  7. def test_python_data_yaml():
  8. testdata = {
  9. "platform": "Android",
  10. "version": "7.0.1",
  11. }
  12. with open(file=yaml_file,mode='w',encoding='utf8') as file:
  13. yaml.dump(testdata,file)

excel文件处理

使用openpyxl 库进行 Excel文件的读写
参考手册: https://openpyxl.readthedocs.io/en/stable/

安装

  1. pip install openpyxl

数据写入到Excel文件

  1. import os
  2. import json
  3. import yaml
  4. root_dir = os.path.dirname( os.path.dirname(os.path.abspath(__file__)))
  5. data_yaml = os.path.join(root_dir,'data/data.yaml')
  6. yaml_file = os.path.join(root_dir,'data/data2.yaml')
  7. excel_file = os.path.join(root_dir,'data/data.xlsx')
  8. from openpyxl import Workbook
  9. def test_python_data_excel():
  10. testdata = [
  11. {"token": "xxxxxxxx"},
  12. {"token": "d1563473-1f0d-4307-9774-6c2ff49c93ab"},
  13. {"token": "d1563473-1f0d-4307-9774-6c2ff49c93a"}
  14. ]
  15. # 创建 Excel 文件
  16. wb = Workbook()
  17. # 创建工作表
  18. wc = wb.create_sheet("用户登录")
  19. # 设置单元格的值
  20. wc.cell(row=1,column=1,value='token')
  21. wc.cell(row=2,column=1,value='d1563473-1f0d-4307-9774-6c2ff49c93ab')
  22. wc.cell(row=3,column=1,value='d1563473-1f0d-4307-9774-6c2ff49c93a')
  23. wc.cell(row=4,column=1,value='xxxxxxxx')
  24. # 保存数据文件
  25. wb.save(filename=excel_file)

思考: 如何通过读取 test_data中的数据保存到 Excel文件中。