python excel和yaml文件的读取封装

时间:2022-10-11 09:44:54

excel

  1. import os
  2. import xlrd
  3.  
  4. PATH = lambda p: os.path.abspath(
  5. os.path.join(os.path.dirname(__file__), p)
  6. )
  7.  
  8. class ExcelData:
  9. def __init__(self, file, sheet="sheet1", title=True):
  10. # 判断文件存在不存在
  11. if os.path.isfile(PATH(file)):
  12. self.file = PATH(file)
  13. self.sheet = sheet
  14. self.title = title
  15. self.data = list()
  16. self.workbook = xlrd.open_workbook(self.file)
  17. else:
  18. raise FileNotFoundError("文件不存在")
  19.  
  20. @property
  21. def get_data(self):
  22. """获取表格数据"""
  23. if not self.data:
  24. # 判断表单名称
  25. if type(self.sheet) not in [int, str]:
  26. raise Exception("表单名称类型错误")
  27. else:
  28. if type(self.sheet) == int:
  29. book = self.workbook.sheet_by_index(self.sheet)
  30. else:
  31. book = self.workbook.sheet_by_name(self.sheet)
  32. # 判断表格是否有表头,有则输出列表嵌套字典形式数据,否则输入列表嵌套列表形式数据
  33. if self.title:
  34. title = book.row_values(0)
  35. for i in range(1, book.nrows):
  36. self.data.append(dict(zip(title, book.row_values(i)))) # 可参考字典章节
  37. else:
  38. for i in range(book.nrows):
  39. self.data.append(book.row_values(i))
  40. return self.data
  41.  
  42. @property
  43. def get_sheets(self):
  44. """获取所有表单,这个在后续会用到"""
  45. book = self.workbook.sheets()
  46. return book

调用操作

  1. infos = ExcelData("htmls/测试用例.xlsx", "登入页面", True).get_data
  2. print(infos)
  3.  
  4. sheets = ExcelData("htmls/测试用例.xlsx").get_sheets
  5. print(sheets)

python excel和yaml文件的读取封装

yaml

  1. import os
  2. import yaml
  3. from yamlinclude import YamlIncludeConstructor
  4.  
  5. YamlIncludeConstructor.add_to_loader_class(loader_class=yaml.FullLoader) # 用于yaml文件嵌套
  6.  
  7. PATH = lambda p: os.path.abspath(os.path.join(
  8. os.path.dirname(__file__), p
  9. ))
  10.  
  11. class YamlData:
  12. def __init__(self, file):
  13. if os.path.isfile(PATH(file)):
  14. self.file = PATH(file)
  15. else:
  16. raise FileNotFoundError("文件不存在")
  17.  
  18. @property # 设置属性,调用data方法时可通过调用属性,不需要带括号
  19. def data(self):
  20. with open(file=self.file, mode="rb") as f:
  21. infos = yaml.load(f, Loader=yaml.FullLoader)
  22. # infos = yaml.load(f)
  23. return infos

调用操作

  1. infos = YamlData("htmls/loginsucess.yaml").data
  2. print(infos)
  3. "D:\Program Files\Python\Python37-32\python.exe" D:/demo/yamldata.py
  4. {'id': 'login_001', 'module': '登入页面', 'title': '登入时账号为空', 'message': '已打开链接', 'testcase': [{'element_info': 'css->[placeholder="请输入账号"]', 'operate_type': 'send_keys', 'keys': 'SSSS', 'info': '点击账号输入框,输入账号'}, {'element_info': 'css->[placeholder="请输入密码"]', 'operate_type': 'send_keys', 'keys': 'XXX', 'info': '点击密码输入框,输入密码'}, {'element_info': 'div->"登 录"', 'operate_type': 'click', 'info': '点击登入菜单'}, {'operate_type': 'is_sleep', 'keys': 3, 'info': '等待进入'}], 'check': None}
  5.  
  6. Process finished with exit code 0

以上就是python excel和yaml文件的读取与封装的详细内容,更多关于python 文件读取与封装的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/mian-1122/p/14235776.html