温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在python中对excel与yaml文件进行读取与封装

发布时间:2021-01-12 14:29:52 来源:亿速云 阅读:508 作者:Leah 栏目:开发技术

本篇文章为大家展示了怎么在python中对excel与yaml文件进行读取与封装,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

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文件进行读取与封装,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI