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中文问题解决方法(总结了多位前人经验,初学者必看)
Mar 13 Python
python实现百度关键词排名查询
Mar 30 Python
深入讲解Java编程中类的生命周期
Feb 05 Python
python实现决策树C4.5算法详解(在ID3基础上改进)
May 31 Python
Python中enumerate函数代码解析
Oct 31 Python
python批量从es取数据的方法(文档数超过10000)
Dec 27 Python
python xpath获取页面注释的方法
Jan 14 Python
解决python tkinter界面卡死的问题
Jul 17 Python
python使用建议与技巧分享(一)
Aug 17 Python
python 如何在测试中使用 Mock
Mar 01 Python
python requests模块的使用示例
Apr 07 Python
人工智能深度学习OpenAI baselines的使用方法
May 20 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/12/05 PHP
PHP缩略图等比例无损压缩,可填充空白区域补充色
2011/06/10 PHP
php处理斐波那契数列非递归方法
2012/02/04 PHP
解析link_mysql的php版
2013/06/30 PHP
php中并发读写文件冲突的解决方案
2013/10/25 PHP
PHP的Laravel框架结合MySQL与Redis数据库的使用部署
2016/03/21 PHP
php判断IP地址是否在多个IP段内
2020/08/18 PHP
js内存泄露的几种情况详细探讨
2013/05/31 Javascript
使用js正则控制input标签只允许输入的值
2013/07/29 Javascript
Extjs实现进度条的两种便捷方式
2013/09/26 Javascript
js将json格式内容转换成对象的方法
2013/11/01 Javascript
JS实现将人民币金额转换为大写的示例代码
2014/02/13 Javascript
js实现div闪烁原理及实现代码
2014/06/24 Javascript
jQuery、layer实现弹出层的打开、关闭功能
2017/06/28 jQuery
js实现图片懒加载效果
2017/07/17 Javascript
js 概率计算(简单版)
2017/09/12 Javascript
JS运动特效之同时运动实现方法分析
2018/01/24 Javascript
[07:40]DOTA2每周TOP10 精彩击杀集锦vol.4
2014/06/25 DOTA
Swift中的协议(protocol)学习教程
2016/07/08 Python
python实现微信发送邮件关闭电脑功能
2018/02/22 Python
python 整数越界问题详解
2019/06/27 Python
Python计算一个点到所有点的欧式距离实现方法
2019/07/04 Python
python画双y轴图像的示例代码
2019/07/07 Python
python 8种必备的gui库
2020/08/27 Python
马来西亚演唱会订票网站:StubHub马来西亚
2018/10/18 全球购物
咖啡店自主创业商业计划书
2014/01/22 职场文书
经典婚礼主持词
2014/03/13 职场文书
重大事项社会稳定风险评估方案
2014/06/15 职场文书
学校运动会广播稿
2014/10/11 职场文书
2016年清明节红领巾广播稿
2015/12/17 职场文书
2016年“世界环境日”校园广播稿
2015/12/18 职场文书
《迟到》教学反思
2016/02/24 职场文书
2019年最新证婚词精选集!
2019/06/28 职场文书
导游词之山东孔庙
2019/11/04 职场文书
Python3.8官网文档之类的基础语法阅读
2021/09/04 Python
使用Nginx+Tomcat实现负载均衡的全过程
2022/05/30 Servers