python使用xlrd实现检索excel中某列含有指定字符串记录的方法


Posted in Python onMay 09, 2015

本文实例讲述了python使用xlrd实现检索excel中某列含有指定字符串记录的方法。分享给大家供大家参考。具体分析如下:

这里利用xlrd,将excel中某列数据中,含有指定字符串的记录取出,并生成用这个字符串命名的txt文件

import os
import xlrd,sys
# input the excel file
Filename=raw_input('input the file name&path:')
if not os.path.isfile(Filename):
  raise NameError,"%s is not a valid filename"%Filename
#open the excel file
bk=xlrd.open_workbook(Filename)
#get the sheets number
shxrange=range(bk.nsheets)
print shxrange
#get the sheets name
for x in shxrange:
  p=bk.sheets()[x].name.encode('utf-8')
  print "Sheets Number(%s): %s" %(x,p.decode('utf-8'))
# input your sheets name
sname=int(raw_input('choose the sheet number:'))
try:
  sh=bk.sheets()[sname]
except:
  print "no this sheet"
  #return None
nrows=sh.nrows
ncols=sh.ncols
# return the lines and col number
print "line:%d col:%d" %(nrows,ncols)
#input the check column
columnnum=int(raw_input('which column you want to check pls input the num(the first colnumn num is 0):'))
while columnnum+1>ncols:
  columnnum=int(raw_input('your num is out of range,pls input again:'))
# input the searching string and column
testin=raw_input('input the string:')
#find the cols and save to a txt
outputfilename=testin + '.txt'
outputfile=open(outputfilename,'w')
#find the rows which you want to select and write to a txt file
for i in range(nrows):
  cell_value=sh.cell_value(i, columnnum)
  if testin in str(cell_value):
    outputs=sh.row_values(i)
    for tim in outputs:
      outputfile.write('%s  ' %(tim))
    outputfile.write('%s' %(os.linesep)) 
outputfile.close()

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
在主机商的共享服务器上部署Django站点的方法
Jul 22 Python
python利用OpenCV2实现人脸检测
Apr 16 Python
Python实现的将文件每一列写入列表功能示例【测试可用】
Mar 19 Python
基于Django URL传参 FORM表单传数据 get post的用法实例
May 28 Python
python 输出所有大小写字母的方法
Jan 02 Python
不到20行代码用Python做一个智能聊天机器人
Apr 19 Python
Python操作redis实例小结【String、Hash、List、Set等】
May 16 Python
pandas 时间格式转换的实现
Jul 06 Python
python中几种自动微分库解析
Aug 29 Python
Python开发之pip安装及使用方法详解
Feb 21 Python
python 实现图像快速替换某种颜色
Jun 04 Python
matlab xlabel位置的设置方式
May 21 Python
Python遍历指定文件及文件夹的方法
May 09 #Python
Python使用chardet判断字符编码
May 09 #Python
python操作ie登陆土豆网的方法
May 09 #Python
Python检测QQ在线状态的方法
May 09 #Python
python常见数制转换实例分析
May 09 #Python
python读写二进制文件的方法
May 09 #Python
Python求导数的方法
May 09 #Python
You might like
浅析iis7.5安装配置php环境
2015/05/10 PHP
对于Laravel 5.5核心架构的深入理解
2018/02/22 PHP
PHP实现数组转JSon和JSon转数组的方法示例
2018/06/14 PHP
js AspxButton的客户端操作
2009/06/26 Javascript
jQuery TextBox自动完成条
2009/07/22 Javascript
javascript下利用arguments实现string.format函数
2010/08/24 Javascript
javascript实现在网页任意处点左键弹出隐藏菜单的方法
2015/05/13 Javascript
JavaScript中用sort()方法对数组元素进行排序的操作
2015/06/09 Javascript
js字符串截取函数slice、substring和substr的比较
2016/05/17 Javascript
微信小程序使用第三方库Underscore.js步骤详解
2016/09/27 Javascript
angular.js指令中transclude选项及ng-transclude指令详解
2017/05/24 Javascript
详解angularJS+Ionic移动端图片上传的解决办法
2017/09/13 Javascript
javascript高级模块化require.js的具体使用方法
2017/10/31 Javascript
详解JavaScript基础知识(JSON、Function对象、原型、引用类型)
2018/01/16 Javascript
详解Angular路由之路由守卫
2018/05/10 Javascript
Vue路由模块化配置的完整步骤
2019/08/14 Javascript
nuxt 自定义 auth 中间件实现令牌的持久化操作
2020/11/05 Javascript
[01:20]DOTA2 齐天大圣至宝动态展示
2016/12/13 DOTA
python使用any判断一个对象是否为空的方法
2014/11/19 Python
python实现简易版计算器
2020/06/22 Python
python使用mysql的两种使用方式
2018/03/07 Python
Python 互换字典的键值对实例
2019/02/12 Python
Django网络框架之HelloDjango项目创建教程
2019/06/06 Python
python爬虫实现中英翻译词典
2019/06/25 Python
python 使用matplotlib 实现从文件中读取x,y坐标的可视化方法
2019/07/04 Python
Python 内存管理机制全面分析
2021/01/16 Python
销售主管岗位职责
2014/02/08 职场文书
人事文员岗位职责
2014/02/16 职场文书
农民工工资承诺书范文
2014/03/31 职场文书
团日活动总结怎么写
2014/06/25 职场文书
小学亲子活动总结
2014/07/01 职场文书
反腐倡廉警示教育活动心得体会
2014/09/04 职场文书
2014年教师工作总结
2014/11/10 职场文书
2015年除四害工作总结
2015/07/23 职场文书
不要在HTML中滥用div
2021/05/08 HTML / CSS
微信小程序结合ThinkPHP5授权登陆后获取手机号
2021/11/23 PHP