excel

import os
import xlrd


PATH = lambda p: os.path.abspath(
  os.path.join(os.path.dirname(__file__), p)
)


class ExcelData:
  def __init__(self, file, sheet="sheet1", title=True):
    # 判断文件存在不存在
    if os.path.isfile(PATH(file)):
      self.file = PATH(file)
      self.sheet = sheet
      self.title = title
      self.data = list()
      self.workbook = xlrd.open_workbook(self.file)
    else:
      raise FileNotFoundError("文件不存在")

  @property
  def get_data(self):
    """获取表格数据"""
    if not self.data:
      # 判断表单名称
      if type(self.sheet) not in [int, str]:
        raise Exception("表单名称类型错误")
      else:
        if type(self.sheet) == int:
          book = self.workbook.sheet_by_index(self.sheet)
        else:
          book = self.workbook.sheet_by_name(self.sheet)
      # 判断表格是否有表头,有则输出列表嵌套字典形式数据,否则输入列表嵌套列表形式数据
      if self.title:
        title = book.row_values(0)
        for i in range(1, book.nrows):
          self.data.append(dict(zip(title, book.row_values(i))))  # 可参考字典章节
      else:
        for i in range(book.nrows):
          self.data.append(book.row_values(i))
    return self.data

  @property
  def get_sheets(self):
    """获取所有表单,这个在后续会用到"""
    book = self.workbook.sheets()
    return book

调用操作

infos = ExcelData("htmls/测试用例.xlsx", "登入页面", True).get_data
print(infos)

sheets = ExcelData("htmls/测试用例.xlsx").get_sheets
print(sheets)

python excel和yaml文件的读取封装

yaml

import os
import yaml
from yamlinclude import YamlIncludeConstructor

YamlIncludeConstructor.add_to_loader_class(loader_class=yaml.FullLoader)  # 用于yaml文件嵌套

PATH = lambda p: os.path.abspath(os.path.join(
  os.path.dirname(__file__), p
))


class YamlData:
  def __init__(self, file):
    if os.path.isfile(PATH(file)):
      self.file = PATH(file)
    else:
      raise FileNotFoundError("文件不存在")

  @property # 设置属性,调用data方法时可通过调用属性,不需要带括号
  def data(self):
    with open(file=self.file, mode="rb") as f:
      infos = yaml.load(f, Loader=yaml.FullLoader)
      # infos = yaml.load(f)
    return infos

调用操作

infos = YamlData("htmls/loginsucess.yaml").data
print(infos)
"D:\Program Files\Python\Python37-32\python.exe" D:/demo/yamldata.py
{'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}

Process finished with exit code 0

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

标签:
python,excel读取与封装,python,yaml读取与封装

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com

评论“python excel和yaml文件的读取封装”

暂无“python excel和yaml文件的读取封装”评论...

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。