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的subprocess模块总结
Nov 07 Python
数据挖掘之Apriori算法详解和Python实现代码分享
Nov 07 Python
解析Mac OS下部署Pyhton的Django框架项目的过程
May 03 Python
Django的信号机制详解
May 05 Python
python爬虫获取多页天涯帖子
Feb 23 Python
Pytorch入门之mnist分类实例
Apr 14 Python
python绘制圆柱体的方法
Jul 02 Python
Python实现模拟浏览器请求及会话保持操作示例
Jul 30 Python
Python UnboundLocalError和NameError错误根源案例解析
Oct 31 Python
解决PySide+Python子线程更新UI线程的问题
Jan 11 Python
使用python实现CGI环境搭建过程解析
Apr 28 Python
Python如何获取文件指定行的内容
May 27 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
PHP面向对象编程快速入门
2006/10/09 PHP
PHP 读取文件的正确方法
2009/04/29 PHP
一个PHP数组应该有多大的分析
2009/07/30 PHP
PHP的Laravel框架中使用消息队列queue及异步队列的方法
2016/03/21 PHP
thinkphp5 URL和路由的功能详解与实例
2017/12/26 PHP
Linux下 php7安装redis的方法
2018/11/01 PHP
php往mysql中批量插入数据实例教程
2018/12/12 PHP
IE浏览器打印的页眉页脚设置解决方法
2009/12/08 Javascript
关于juqery radio写法的兼容性问题(新老版本jquery)
2010/06/14 Javascript
jquery 学习之二 属性 文本与值(text,val)
2010/11/25 Javascript
浅谈checkbox的一些操作(实战经验)
2013/11/20 Javascript
JS实现日期加减的方法
2013/11/29 Javascript
js实现点击切换TAB标签实例
2015/08/21 Javascript
javascript中sort排序实例详解
2016/07/24 Javascript
js实现九宫格拼图小游戏
2017/02/13 Javascript
node结合swig渲染摸板的方法
2018/04/11 Javascript
通过说明与示例了解js五种设计模式
2019/06/17 Javascript
Javascript幻灯片播放功能实现过程解析
2020/05/07 Javascript
构建Python包的五个简单准则简介
2015/06/15 Python
理解Python中的With语句
2016/03/18 Python
举例讲解Python中的迭代器、生成器与列表解析用法
2016/03/20 Python
Python编写一个闹钟功能
2017/07/11 Python
Python中矩阵库Numpy基本操作详解
2017/11/21 Python
用Python进行简单图像识别(验证码)
2018/01/19 Python
Python机器学习算法之k均值聚类(k-means)
2018/02/23 Python
python3中zip()函数使用详解
2018/06/29 Python
HTML5开发动态音频图的实现
2020/07/02 HTML / CSS
Sneaker Studio罗马尼亚网站:购买运动鞋
2018/11/04 全球购物
Glamest意大利:女性在线奢侈品零售店
2019/04/28 全球购物
Skechers越南官方网站:来自美国的运动休闲品牌
2021/02/22 全球购物
如何在Oracle中查看各个表、表空间占用空间的大小
2015/10/31 面试题
运动会标语
2014/06/21 职场文书
伊琍体标语
2014/06/25 职场文书
2014业务员年终工作总结
2014/12/09 职场文书
五一劳动节慰问信
2015/02/14 职场文书
导游词之湖北梁子湖
2019/11/07 职场文书