zoukankan      html  css  js  c++  java
  • 读取yaml和excel配置文件

    1.读取yaml配置文件

    from os.path import exists
    from yaml import safe_load_all, safe_load
    
    
    class File(object):
    
        def __init__(self, file_path: str):
            if not exists(file_path):
                raise FileNotFoundError
            self._file_path = file_path
            self._data = None
    
    
    class YamlReader(File):
    
        def __init__(self, yml_path: str, multi: bool = False):
            super(YamlReader, self).__init__(yml_path)
            self._multi = multi
    
        @property
        def data(self):
            if not self._data:
                with open(self._file_path, 'rb') as fp:
                    if self._multi:
                        self._data = list(safe_load_all(fp))
                    else:
                        self._data = safe_load(fp)
            return self._data
    

    1.读取excel配置文件

    from os.path import exists
    from xlrd import open_workbook
    
    
    class File(object):
    
        def __init__(self, file_path: str):
            if not exists(file_path):
                raise FileNotFoundError
            self._file_path = file_path
            self._data = None
    
    
    class ExcelReader(File):
        def __init__(self, excel_path: str, sheet: [str, int], excel_title: bool = False):
            super(ExcelReader, self).__init__(excel_path)
            self._sheet = sheet
            self._excel_title = excel_title
            self._data = []
    
        @property
        def data(self):
            if not self._data:
                work_book = open_workbook(self._file_path)
                if not isinstance(self._sheet, (str, int)):
                    raise TypeError("excel 表格不,{}不存在".format(self._sheet))
                if isinstance(self._sheet, int):
                    s = work_book.sheet_by_index(self._sheet)
                else:
                    s = work_book.sheet_by_name(self._sheet)
                if self._excel_title:
                    title = s.row_values(0)
                    for col in range(1, s.nrows):
                        self._data.append(dict(zip(title, s.row_values(col))))
                else:
                    for col in range(1, s.nrows):
                        self._data.append(s.row_values(col))
            return self._data
    

      

  • 相关阅读:
    KVM之一:安装准备(基于CentOS6.7)
    Nginx技巧——Nginx/Apache下禁止指定目录运行PHP脚本(转自运维之美)
    (转)关于 awk 的 pattern(模式)
    form验证里使用request 和前端倒计时
    restful 在接口中获取 传过来的值
    django使用celery
    celery
    restful 在接口中获取 request.user
    码云创建一个仓库
    django中local_settings的配置
  • 原文地址:https://www.cnblogs.com/yoyo1216/p/14809077.html
Copyright © 2011-2022 走看看