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实现判断数组是否包含指定元素的方法
Jul 15 Python
Python解析树及树的遍历
Feb 03 Python
Python yield 使用方法浅析
May 20 Python
详解如何在python中读写和存储matlab的数据文件(*.mat)
Feb 24 Python
python编写暴力破解zip文档程序的实例讲解
Apr 24 Python
Python 对输入的数字进行排序的方法
Jun 23 Python
对tensorflow 的模型保存和调用实例讲解
Jul 28 Python
利用django+wechat-python-sdk 创建微信服务器接入的方法
Feb 20 Python
详解Python3 中的字符串格式化语法
Jan 15 Python
Django中从mysql数据库中获取数据传到echarts方式
Apr 07 Python
python shapely.geometry.polygon任意两个四边形的IOU计算实例
Apr 12 Python
关于Python 解决Python3.9 pandas.read_excel(‘xxx.xlsx‘)报错的问题
Nov 28 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版(5)
2006/10/09 PHP
php获取操作系统语言代码
2013/11/04 PHP
PHP开发框架laravel安装与配置教程
2015/03/13 PHP
php命令行(cli)下执行PHP脚本文件的相对路径的问题解决方法
2015/05/25 PHP
php查询操作实现投票功能
2016/05/09 PHP
PHP7实现和CryptoJS的AES加密方式互通示例【AES-128-ECB加密】
2019/06/08 PHP
ext读取两种结构的xml的代码
2008/11/05 Javascript
jquery autocomplete自动完成插件的的使用方法
2010/08/07 Javascript
Jquery知识点二 jquery下对数组的操作
2011/01/15 Javascript
查找iframe里元素的方法可传参
2013/09/11 Javascript
如何获取网站icon有哪些可行的方法
2014/06/05 Javascript
jQuery中Ajax的load方法详解
2015/01/14 Javascript
JQuery鼠标移到小图显示大图效果的方法
2015/06/10 Javascript
javascript中活灵活现的Array对象详解
2016/11/30 Javascript
js实现做通讯录的索引滑动显示效果和滑动显示锚点效果
2017/02/18 Javascript
JavaScript中数组Array方法详解
2017/02/27 Javascript
js判断是否是手机页面
2017/03/17 Javascript
jQuery实现简单的滑动导航代码(移动端)
2017/05/22 jQuery
vue-cli配置文件——config篇
2018/01/04 Javascript
vue中watch和computed的区别与使用方法
2020/08/23 Javascript
[46:32]Fnatic vs OG 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
初学Python实用技巧两则
2014/08/29 Python
Python的Django框架下管理站点的基本方法
2015/07/17 Python
Python中条件判断语句的简单使用方法
2015/08/21 Python
Tensorflow加载预训练模型和保存模型的实例
2018/07/27 Python
python pickle存储、读取大数据量列表、字典数据的方法
2019/07/07 Python
Python爬虫:将headers请求头字符串转为字典的方法
2019/08/21 Python
SKECHERS斯凯奇中国官网:来自美国的运动休闲品牌
2018/11/14 全球购物
加拿大在线隐形眼镜和眼镜店:VisionPros
2019/10/06 全球购物
大学班级干部的自我评价分享
2014/02/10 职场文书
爱国主义教育活动总结
2014/05/07 职场文书
舞蹈教育学专业自荐信
2014/06/15 职场文书
小学教师师德整改措施
2014/09/29 职场文书
走群众路线学习笔记
2014/11/06 职场文书
2014年银行员工工作总结
2014/11/12 职场文书
小学音乐教师个人工作总结
2015/02/05 职场文书