1.读取yaml配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 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 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)