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单链表的简单实现方法
Sep 23 Python
Python标准库内置函数complex介绍
Nov 25 Python
python对数组进行反转的方法
May 20 Python
Python实现的人工神经网络算法示例【基于反向传播算法】
Nov 11 Python
python+matplotlib绘制旋转椭圆实例代码
Jan 12 Python
对python中执行DOS命令的3种方法总结
May 12 Python
python实现文件助手中查看微信撤回消息
Apr 29 Python
python图形绘制奥运五环实例讲解
Sep 14 Python
浅谈python中统计计数的几种方法和Counter详解
Nov 07 Python
python 创建一维的0向量实例
Dec 02 Python
Python读写操作csv和excle文件代码实例
Mar 16 Python
python 5个顶级异步框架推荐
Sep 09 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中inlcude()性能对比详解
2012/09/16 PHP
php实现无限级分类
2014/12/24 PHP
thinkPHP框架实现图像裁剪、缩放、加水印的方法
2017/03/14 PHP
简单JS代码压缩器
2006/10/12 Javascript
jQuery如何取id有.的值一般的方法是取不到的
2014/04/18 Javascript
Javascript正则控制文本框只能输入整数或浮点数
2014/09/02 Javascript
jQuery前端分页示例分享
2015/02/10 Javascript
教你如何使用firebug调试功能了解javascript闭包和this
2015/03/04 Javascript
JavaScript实现的select点菜功能示例
2017/01/16 Javascript
Vue单文件组件基础模板小结
2017/08/10 Javascript
ES6中Array.includes()函数的用法
2017/09/20 Javascript
three.js 入门案例详解
2018/01/23 Javascript
vue 指令之气泡提示效果的实现代码
2018/10/18 Javascript
vue框架下部署上线后刷新报404问题的解决方案(推荐)
2019/04/03 Javascript
LayUI动态设置checkbox不显示的解决方法
2019/09/02 Javascript
Linux系统上Nginx+Python的web.py与Django框架环境
2015/12/25 Python
python 性能优化方法小结
2017/03/31 Python
python win32 简单操作方法
2017/05/25 Python
对python多线程SSH登录并发脚本详解
2019/02/14 Python
Python检测数据类型的方法总结
2019/05/20 Python
Django 对象关系映射(ORM)源码详解
2019/08/06 Python
python3 enum模块的应用实例详解
2019/08/12 Python
浅谈Keras的Sequential与PyTorch的Sequential的区别
2020/06/17 Python
Django返回HTML文件的实现方法
2020/09/17 Python
有关HTML5页面在iPhoneX适配问题
2017/11/13 HTML / CSS
HTML5 SEO优化的一些建议
2020/08/27 HTML / CSS
英国标准协会商店:BSI Shop
2019/02/25 全球购物
英国买鞋网站:Charles Clinkard
2019/11/14 全球购物
大学生护理专业自荐信
2013/10/03 职场文书
同学聚会欢迎辞
2014/01/14 职场文书
酒店管理求职信范文
2014/04/06 职场文书
园艺师求职信
2014/04/27 职场文书
2014年汽车销售工作总结
2014/12/01 职场文书
民事上诉状范文
2015/05/22 职场文书
python实现对doc、txt、xls等文档的读写操作
2022/04/02 Python
在容器中使用nginx搭建上传下载服务器
2022/05/11 Servers