如何用python提取Excel中指定列名的数据

#coding=utf-8 import xlrd,chardet,traceback #根据列名获取相应序号 def getColumnIndex(table,columnName): columnIndex=None for i in range(table.ncols): if(table.cell_value(0,i)==columnName): columnIndex=i break return columnIndex #根据Excel中sheet名称读取数据 def readExcelDataByName(fileName,sheetName): table=None try: data=xlrd.open_workbook(fileName) table=data.sheet_by_name(sheetName) except Exception: pass return table if __name__==‘__main__‘: #example xlsfile=r‘/Users//Desktop/python/房源清单-(截止1031).xlsx‘ xlssheet=‘Sheet1‘ table=readExcelDataByName(xlsfile,xlssheet) #获取第一行的值 chanpinleixing=table.cell_value(1,getColumnIndex(table,‘产品类型‘)) fangyuanmingcheng=table.cell_value(1,getColumnIndex(table,‘房源名称‘)) print(u‘产品类型为:%s‘%(chanpinleixing)) print(u‘房源名称为:%s‘%(fangyuanmingcheng))

相关推荐