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 文件操作实现代码
Oct 07 Python
Python实现的数据结构与算法之队列详解
Apr 22 Python
详解 Python 读写XML文件的实例
Aug 02 Python
浅谈Django自定义模板标签template_tags的用处
Dec 20 Python
python网络爬虫学习笔记(1)
Apr 09 Python
查看django版本的方法分享
May 14 Python
Python线程下使用锁的技巧分享
Sep 13 Python
python利用多种方式来统计词频(单词个数)
May 27 Python
linux下python中文乱码解决方案详解
Aug 28 Python
Numpy之reshape()使用详解
Dec 26 Python
Django更新models数据库结构步骤
Apr 01 Python
如何用python绘制雷达图
Apr 24 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
Home Coffee Roasting
2021/03/03 咖啡文化
php实现ping
2006/10/09 PHP
第二节--PHP5 的对象模型
2006/11/16 PHP
php_screw 1.5:php加密: 安装与使用详解
2013/06/20 PHP
php实现与python进行socket通信的方法示例
2017/08/30 PHP
js location.replace与location.reload的区别
2010/09/08 Javascript
Prototype源码浅析 String部分(二)
2012/01/16 Javascript
Get中文乱码IE浏览器Get中文乱码解决方案
2013/12/26 Javascript
javascript实现检验的各种规则
2015/07/31 Javascript
js阻止浏览器默认行为的简单实例
2016/05/15 Javascript
jQuery 生成svg矢量二维码
2016/08/09 Javascript
详解node单线程实现高并发原理与node异步I/O
2017/09/21 Javascript
node实现登录图片验证码的示例代码
2018/04/20 Javascript
详解Ubuntu安装angular-cli遇到的坑
2018/09/08 Javascript
Angular4 Select选择改变事件的方法
2018/10/09 Javascript
JS使用对象的defineProperty进行变量监控操作示例
2019/02/02 Javascript
vue中axios实现数据交互与跨域问题
2019/05/12 Javascript
vue基本使用--refs获取组件或元素的实例
2019/11/07 Javascript
基于 Vue 的 Electron 项目搭建过程图文详解
2020/07/22 Javascript
利用Anaconda完美解决Python 2与python 3的共存问题
2017/05/25 Python
python简易实现任意位数的水仙花实例
2018/11/13 Python
Python3爬虫学习之MySQL数据库存储爬取的信息详解
2018/12/12 Python
python tkinter库实现气泡屏保和锁屏
2019/07/29 Python
使用Python打造一款间谍程序的流程分析
2020/02/21 Python
宝塔面板成功部署Django项目流程(图文)
2020/06/22 Python
Python字典实现伪切片功能
2020/10/28 Python
香港草莓网土耳其网站:Strawberrynet TR
2017/03/02 全球购物
来自世界各地的饮料:Flavourly
2019/05/06 全球购物
EJB实例的生命周期
2016/10/28 面试题
小学美术教学反思
2014/02/01 职场文书
人力管理专业毕业生求职信
2014/02/27 职场文书
书香家庭事迹材料
2014/05/09 职场文书
大学拉赞助协议书范文
2014/09/26 职场文书
房产转让协议书(2014版)
2014/09/30 职场文书
2015年世界环境日活动总结
2015/02/11 职场文书
win10搭建配置ftp服务器的方法
2022/08/05 Servers