python excel取列_python读取excel按列获取设置字符编码

根据列名读取相应的值,代码如下:

1 #!/usr/bin/python 2 # coding=utf-8 3 __author__ = 'Paul' 4 import xlrd 5 import chardet 6 import traceback 7 def getColumnIndex(table, columnName): 8 columnIndex = None 9 #print table10 for i in range(table.ncols):11 #print columnName12 #print table.cell_value(0, i)13 if(table.cell_value(0, i) == columnName):14 columnIndex = i15 break16 return columnIndex17 def readExcelDataByName(fileName, sheetName):18 #print fileName19 table = None20 errorMsg = ""21 try:22 data = xlrd.open_workbook(fileName)23 table = data.sheet_by_name(sheetName)24 except Exception, msg:25 errorMsg = msg26 return table, errorMsg27 def readExcelDataByIndex(fileName, sheetIndex):28 table = None29 errorMsg = ""30 try:31 data = xlrd.open_workbook(fileName)32 table = data.sheet_by_index(sheetIndex)33 except Exception, msg:34 errorMsg = msg35 return table, errorMsg36 if __name__ == '__main__':37 #example38 xlsfile= 'F:/test_AutoTesting/TestCase/RunList.xlsx'39 table = readExcelDataByName(xlsfile, 'Sheet1')[0]40 #获取第一行的值 41 testcase_id = table.cell_value(1, getColumnIndex(table,'TestCaseID'))42 app_config = table.cell_value(1, getColumnIndex(table,'APPConfig'))43 44 print u'测试用例ID为:%s'%(testcase_id)45 print u'配置信息为:%s'%(app_config)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值