python3读取excel表数据(可指定sheet表读取及设置读取后返回的数据类型)

本文介绍了如何使用Python3从Excel文件中读取数据,特别是指定sheet表读取,并能将数据转换为JSON格式。提供了一个包含读取Excel的源码示例,以及三种调用情况,包括正常读取全部sheet和指定sheet的情况。
摘要由CSDN通过智能技术生成

1、前述介绍

我在测试一个智能对话项目时需要评估对话的准确率,就设计了一些问题放到excel表中,读取问题并触发问答后把响应信息按需要的数据写入到另外一个excel中。基于这个,我分别写了读excel函数和写excel函数。
使用方法、注意事项、参数介绍等均在源码中有注释,不在这里赘述,下面直接看源码。

写excel表文章地址:https://blog.csdn.net/u013478829/article/details/109401340

2、源码

# 读excel文件
def readExcel(readExcelNameOrPath='', readSheets=None, contentType='json'):
    import xlrd, os, json
    # 参数说明
    # readExcelNameOrPath  读取excel的文件名称(aa.xls)或文件的绝对路径(r"E:\test\aa.xls"),如果为文件名称,必须放到当前目录下
    # readSheets   要读取的sheet表名称,如果不设置默认读取第一个表,如果设置为all代表读取全部的sheet的内容,注意:只有['all']这种情况下才读取全部sheet
    # contentType   设置返回值的类型
    # 注意:如果路径中出现转义字符,如\t,\n等,路径前面加r,如 r"E:\test\aa.xls"
    # 返回值格式:{sheetName:[{"row": 0, "col": 0, "content": content}, ..., {"row": m, "col": n, "content": content}]}

    readExcelNameOrPath = str(readExcelNameOrPath)
    contentType = str(contentType).lower()
    readSheetsContent = {
   }  # key为sheet表名称,value为对应sheet的内容list
    readSheetsInfoList = []

    try:
        # 处理传参错误时的情况
        if not os.path.isfile(os.path.abspath(readExcelNameOrPath)):
            readSheetsContent['文件错误'] = '文件名称或路径设置错误[%s]' % (readExcelNameOrPath, )
            readSheetsContent['规则'] = '只设置文件名称,需要将文件放到当前目录下;如果设置路径,必须为文件的绝对路径,如%s' % (r"E:\test\aa.xls", )
            if contentType != 'dict':
                readSheetsContent = json.dumps(readSheetsContent, ensure_ascii=False)
            return readSheetsContent
        if not (os.path.abspath(readExcelNameOrPath).endswith('.xls') or os.path.abspath(readExcelNameOrPath).endswith('.xlsx')):
            readSheetsContent['文件错误'] = ' 文件格式错误,必须为excel文件,后缀名只能为.xls和.xlsx'
            if contentType != 'dict':
                readSheetsContent = json.dumps(readSheetsContent, ensure_ascii=False)
            return readSheetsContent
        # 打开文件
        excelInfo = xlrd.open_workbook(readExcelNameOrPath)

        # 需要读取的sheet表参数传参异常时默认读取第一个
        if not isinstance(readSheets, list) or not readSheets:
            temSheet = excelInfo.sheet_by_index(0)
            readSheetsInfoList.append(temSheet)
        else:
            # 如果设置为all,处理全部sheet表格
            if len(readSheets) == 1 and str(readSheets[0]).lower() == 'all':
                for sheetIndex in range(0, excelInfo.nsheets):
                    temSheet = excelInfo.sheet_by_index(sheetIndex)
                    readSheetsInfoList.append(temSheet)

            # 按设置的sheet表名称进行处理
            else:
                for sheetName in readSheets:
                    sheetName = str(sheetName)
                    try:
                        temSheet = excelInfo.sheet_by_name(sheetName)
                        readSheetsInfoList.append(temSheet)
                    except:
                        # sheet表名称找不到时返回提示信息,注意:区分大小写
                        readSheetsContent[sheetName] = ['该sheet表不存在']

        # 开始处理sheet表数据
        for sheetInfo in readSheetsInfoList:
            dataRows = sheetInfo.nrows
            dataCols= sheetInfo.ncols
            if dataRows > 0:
                temDict = []
                for row in range(0, dataRows):
                    for col in range(0, dataCols):
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值