码迷,mamicode.com
首页 > 其他好文 > 详细

测试组件 -ExcelHandler

时间:2020-05-14 15:21:46      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:file_path   读取excel   imp   for   index   work   book   excel   sel   

"""
处理Excel
"""

import xlrd
from conf import settings
from utils.LogHandler import logger


class ExcelOperate(object):

    def __init__(self, file_path, sheet_by_index=0):
        self.file_path = file_path
        self.sheet_by_index = sheet_by_index
        book = xlrd.open_workbook(self.file_path)
        self.sheet = book.sheet_by_index(self.sheet_by_index)

    def get_excel(self):
        """ 获取Excel数据 """
        # l = []

        title = self.sheet.row_values(0)
        # print(title)
        # for row in range(1, self.sheet.nrows):
        #     l.append(dict(zip(title, self.sheet.row_values(row))))
        # return l
        # print(1111, self.sheet.nrows)
        l = [dict(zip(title, self.sheet.row_values(row))) for row in range(1, self.sheet.nrows)]
        logger().info(‘读取Excel 成功,数据已返回‘)
        return l

if __name__ == ‘__main__‘:
    excel_data_list = ExcelOperate(settings.FILE_PATH, 2).get_excel()
    print(excel_data_list)













测试组件 -ExcelHandler

标签:file_path   读取excel   imp   for   index   work   book   excel   sel   

原文地址:https://www.cnblogs.com/hude/p/12888453.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!