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 相关文章推荐
Python和php通信乱码问题解决方法
Apr 15 Python
Linux CentOS7下安装python3 的方法
Jan 21 Python
python web.py开发httpserver解决跨域问题实例解析
Feb 12 Python
Django 实现购物车功能的示例代码
Oct 08 Python
Python pyinotify模块实现对文档的实时监控功能方法
Oct 13 Python
pycharm远程开发项目的实现步骤
Jan 20 Python
Django实现文件上传下载
Oct 06 Python
python批量替换文件名中的共同字符实例
Mar 05 Python
spyder 在控制台(console)执行python文件,debug python程序方式
Apr 20 Python
详解pytorch中squeeze()和unsqueeze()函数介绍
Sep 03 Python
pycharm Tab键设置成4个空格的操作
Feb 26 Python
OpenCV3.3+Python3.6实现图片高斯模糊
May 18 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
整理的9个实用的PHP库简介和下载
2010/11/09 PHP
php之可变变量的实例详解
2017/09/12 PHP
PHP面向对象程序设计中的self、static、parent关键字用法分析
2019/08/14 PHP
YII2框架使用控制台命令的方法分析
2020/03/18 PHP
JS对URL字符串进行编码/解码分析
2008/10/25 Javascript
flexigrid 类似ext grid的JS表格代码
2010/07/17 Javascript
JQuery弹出炫丽对话框的同时让背景变灰色
2014/05/22 Javascript
jQuery判断checkbox是否选中的3种方法
2014/08/12 Javascript
javascript实现页面刷新时自动清空表单并选中的方法
2015/07/18 Javascript
详解JavaScript中常用的函数类型
2015/11/18 Javascript
省市联动效果的简单实现代码(推荐)
2016/06/06 Javascript
JavaScript性能优化总结之加载与执行
2016/08/11 Javascript
Javascript中indexOf()和lastIndexOf应用方法实例
2016/08/24 Javascript
JS中append字符串包含onclick无效传递参数失败的解决方案
2016/12/26 Javascript
JS如何实现在页面上快速定位(锚点跳转问题)
2017/08/14 Javascript
自制简易打赏功能的实例
2017/09/02 Javascript
JS鼠标3次点击事件实现代码及扩展思路
2017/09/12 Javascript
Vue中的无限加载vue-infinite-loading的方法
2018/04/08 Javascript
详解webpack4多入口、多页面项目构建案例
2018/05/25 Javascript
JS开发自己的类库实例分析
2019/08/28 Javascript
layer.open 子页面弹出层向父页面传输数据的例子
2019/09/26 Javascript
Vue.js 中制作自定义选择组件的代码附演示demo
2020/02/28 Javascript
Python中转换角度为弧度的radians()方法
2015/05/18 Python
浅谈Python的异常处理
2016/06/19 Python
利用numpy+matplotlib绘图的基本操作教程
2017/05/03 Python
windows10下安装TensorFlow Object Detection API的步骤
2019/06/13 Python
基于 Django 的手机管理系统实现过程详解
2019/08/16 Python
python selenium循环登陆网站的实现
2019/11/04 Python
PyCharm下载和安装详细步骤
2019/12/17 Python
python各层级目录下import方法代码实例
2020/01/20 Python
基于Numba提高python运行效率过程解析
2020/03/02 Python
CSS3使用多列制作瀑布流
2016/05/10 HTML / CSS
Notino意大利:购买香水和化妆品
2018/11/14 全球购物
ALDO美国官网:加拿大女鞋品牌
2018/12/28 全球购物
学生发电厂实习自我鉴定
2013/09/22 职场文书
2014年党支部书记工作总结
2014/12/04 职场文书