首页 > 其他分享 >read_file

read_file

时间:2022-10-08 17:58:42浏览次数:36  
标签:__ file read self conf path

# coding=utf8
import configparser
import xlrd
import yaml


class Read_Excel:
def __init__(self, file_path, num=0):
self.wb = xlrd.open_workbook(file_path)
self.sheet = self.wb.sheet_by_index(num)

def read_excl(self, x, y):
return self.sheet.cell_value(x, y)


class Read_Conf:
def __init__(self, file_path):
self.file_path = file_path
self.conf = configparser.ConfigParser()
self.conf.read(self.file_path, encoding='utf8')

def read_conf(self, section, option):
return self.conf.get(section, option)

def get_items(self, item):
return dict(self.conf.items(item))

def add(self, section):
if section not in self.conf.sections():
self.conf.add_section(section)

def write(self, section, option, value=None):
self.add(section)
self.conf.set(section, option, value)
with open(self.file_path, 'w', encoding='utf8') as f:
self.conf.write(f)


class Read_Yaml:
def __init__(self, file_path):
self.file_path = file_path
self.get_yd = None

def read_yaml(self, encoding='utf-8'):
with open(self.file_path, encoding=encoding) as f:
self.get_yd = yaml.load(f, Loader=yaml.FullLoader)
return self.get_yd

def write_yaml(self, json_data, encoding='utf-8'):
with open(self.file_path, 'w', encoding=encoding) as f:
yaml.dump(json_data, f)



if __name__ == "__main__":
# a = Read_Conf(r'D:\pypro\\report\xml\environment.properties')
# a.write('env1', 'Browser', 'Chrome')
# print(a.read_conf('env1', 'Browser'))
# a = Read_Excel('home_page.xls')
# print(a.read_excl(0, 1), type(a))
a = Read_Conf(r'D:\pypro\\data\url_path.conf')
print(a.read_conf('url_path', 'host_url'))
print(a.get_items('url_path'))
print(a.read_conf('header', 'Cookie'))
print(a.get_items('header'))
# a = Read_Yaml(r'D:\pypro\\data\gbf.yaml')
# a.write_yaml(json_data1)
# data = a.read_yaml()
# list_state = []
# for i in data['state'].split(','):
# list_state.append(int(i))
# print(list_state)
# print(data['add_data_gbf'], type(data['add_data_gbf']))

标签:__,file,read,self,conf,path
From: https://www.cnblogs.com/littleyang/p/16769716.html

相关文章