configparser
# -*- coding: utf-8 -*-
import configparser
class ConfigUtils:
config = configparser.ConfigParser()
def read(self, filename):
"""Read and parse a filename or an iterable of filenames."""
self.config.read(filename, encoding="utf-8-sig")
def get(self, _options, _section='server'):
"""Get an option value for a given section."""
try:
value = self.config.get(section=_section, option=_options, fallback="This section not exist this option's value.")
except Exception as e:
print(e)
value = None
return value
def get_options_key_value(self, _section):
"""Return a list of (name, value) tuples for each option in a section."""
return self.config.items(section=_section)
def get_all_section(self):
"""Return a list of section names, excluding [DEFAULT]."""
return self.config.sections()
def get_options_by_section(self, _section):
"""Return a list of option names for the given section name."""
return self.config.options(section=_section)
def assert_section_in_config(self, _section):
"""assert if a section exists."""
return _section in self.config
def assert_options_in_section(self, _section, _options):
"""assert if a option exist in section."""
return _options in self.config[_section]
if __name__ == '__main__':
import os
filename = os.path.split(os.path.realpath(__file__))[0] + './config.ini'
configUtil = ConfigUtils()
configUtil.read(filename=filename)
print(configUtil.get('option1', 'PROJECT-Section1'))
print(configUtil.get('option3', 'PROJECT-Section1'))
print(configUtil.get_options_key_value('PROJECT-Section1'))
print(configUtil.get_all_section())
print(configUtil.get_options_by_section('PROJECT-Section1'))
print(configUtil.assert_section_in_config('PROJECT-Section1'))
print(configUtil. assert_options_in_section('PROJECT-Section1', 'Test_Type'))
print(configUtil. assert_options_in_section('PROJECT-Section1', 'Test'))
config.ini
[PROJECT-Section1]
Test_Type = 'automation'
option1 = 'hello world'
option2 = "This is Section1's option2"
[PROJECT-Section2]
Test_Type = 'automation22222'
option1 = 'hello world222222'
option2 = "This is Section2's option2"
[PROJECT-Section3]
Test_Type = '3333333333'
openpyxl
# -*- coding: utf-8 -*-
import openpyxl
class ExcelUtils:
workBook = None
workSheet = None
def load_excel(self, filename):
"""open the given filename and return the workbook."""
try:
self.workBook = openpyxl.load_workbook(filename)
except BaseException as e:
print(e)
def get_sheet_by_name(self, name):
"""returns a worksheet by its name."""
try:
# !DeprecationWarning: Call to deprecated function get_sheet_by_name (Use wb[sheetname]).
self.workSheet = self.workBook[name]
except BaseException as e:
print(e)
def get_sheet_by_index(self, index=0):
# !DeprecationWarning: Call to deprecated function get_sheet_names (Use wb.sheetnames).
sheet_names = self.workBook.sheetnames
print(sheet_names)
self.workSheet = self.workBook[sheet_names[index]]
def get_cell_value(self, col, row):
"""get value for specified row and column."""
try:
return self.workSheet.cell(column=col, row=row).value
except BaseException as e:
print(e)
return None
def get_cell_value_by_xy(self, str):
"""get value for coordinate, such as 'A1'."""
try:
return self.workSheet[str].value
except BaseException as e:
print(e)
return None
def get_sheet_rows(self):
"""get current sheet max row number."""
return self.workSheet.max_row
def get_sheet_cols(self):
"""get current sheet max column number."""
return self.workSheet.max_column
def write_data(self, row, col, value, path):
"""Write data to the specified rows and columns of an Excel file in the specified xlsx format."""
try:
self.workSheet = self.workBook.active
self.workSheet.cell(column=col, row=row, value=value)
self.workBook.save(path)
except BaseException as e:
print(e)
return None
def get_excel_data(self):
"""get current sheet all data, return list."""
return tuple(self.workSheet.values)
def get_row_value(self, row):
"""get current sheet row value, return list"""
return self.get_excel_data()[row]
def get_col_value(self, col='A'):
"""get current sheet column value, return list."""
col_list = []
for i in self.workSheet[col]:
col_list.append(i.value)
return col_list
def get_row_num(self, case_id):
"""get case id row number."""
num = 1
col_data = self.get_col_value()
for data in col_data:
if case_id == data:
return num
num += 1
return 0
if __name__ == '__main__':
import os
filename = os.path.split(os.path.realpath(__file__))[0] + '/tempdata/data.xlsx'
excelUtils = ExcelUtils()
excelUtils.load_excel(filename=filename)
excelUtils.get_sheet_by_name("data")
# excelUtils.get_sheet_by_index(index=0)
value = excelUtils.get_cell_value(col=1, row=1)
print(value)
原文始发于微信公众号(不知名菜鸟):configparser、openpyxl模块常用方法封装
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/240590.html