溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在python中對(duì)excel與yaml文件進(jìn)行讀取與封裝

發(fā)布時(shí)間:2021-01-12 14:29:52 來(lái)源:億速云 閱讀:531 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

本篇文章為大家展示了怎么在python中對(duì)excel與yaml文件進(jìn)行讀取與封裝,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

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):
    """獲取表格數(shù)據(jù)"""
    if not self.data:
      # 判斷表單名稱
      if type(self.sheet) not in [int, str]:
        raise Exception("表單名稱類型錯(cuò)誤")
      else:
        if type(self.sheet) == int:
          book = self.workbook.sheet_by_index(self.sheet)
        else:
          book = self.workbook.sheet_by_name(self.sheet)
      # 判斷表格是否有表頭,有則輸出列表嵌套字典形式數(shù)據(jù),否則輸入列表嵌套列表形式數(shù)據(jù)
      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))))  # 可參考字典章節(jié)
      else:
        for i in range(book.nrows):
          self.data.append(book.row_values(i))
    return self.data

  @property
  def get_sheets(self):
    """獲取所有表單,這個(gè)在后續(xù)會(huì)用到"""
    book = self.workbook.sheets()
    return book

調(diào)用操作

infos = ExcelData("htmls/測(cè)試用例.xlsx", "登入頁(yè)面", True).get_data
print(infos)

sheets = ExcelData("htmls/測(cè)試用例.xlsx").get_sheets
print(sheets)

怎么在python中對(duì)excel與yaml文件進(jìn)行讀取與封裝

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 # 設(shè)置屬性,調(diào)用data方法時(shí)可通過(guò)調(diào)用屬性,不需要帶括號(hào)
  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

調(diào)用操作

infos = YamlData("htmls/loginsucess.yaml").data
print(infos)
"D:\Program Files\Python\Python37-32\python.exe" D:/demo/yamldata.py
{'id': 'login_001', 'module': '登入頁(yè)面', 'title': '登入時(shí)賬號(hào)為空', 'message': '已打開(kāi)鏈接', 'testcase': [{'element_info': 'css->[placeholder="請(qǐng)輸入賬號(hào)"]', 'operate_type': 'send_keys', 'keys': 'SSSS', 'info': '點(diǎn)擊賬號(hào)輸入框,輸入賬號(hào)'}, {'element_info': 'css->[placeholder="請(qǐng)輸入密碼"]', 'operate_type': 'send_keys', 'keys': 'XXX', 'info': '點(diǎn)擊密碼輸入框,輸入密碼'}, {'element_info': 'div->"登 錄"', 'operate_type': 'click', 'info': '點(diǎn)擊登入菜單'}, {'operate_type': 'is_sleep', 'keys': 3, 'info': '等待進(jìn)入'}], 'check': None}

Process finished with exit code 0

上述內(nèi)容就是怎么在python中對(duì)excel與yaml文件進(jìn)行讀取與封裝,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI