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 26 Python
python使用socket创建tcp服务器和客户端
Apr 12 Python
python3.7.0的安装步骤
Aug 27 Python
使用django-guardian实现django-admin的行级权限控制的方法
Oct 30 Python
python-Web-flask-视图内容和模板知识点西宁街
Aug 23 Python
python 命令行传入参数实现解析
Aug 30 Python
Python3.7.0 Shell添加清屏快捷键的实现示例
Mar 23 Python
django有哪些好处和优点
Sep 01 Python
Python列表嵌套常见坑点及解决方案
Sep 30 Python
python uuid生成唯一id或str的最简单案例
Jan 13 Python
Python Django / Flask如何使用Elasticsearch
Apr 19 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中绘制图像的一些函数总结
2014/11/19 PHP
smarty模板引擎从配置文件中获取数据的方法
2015/01/22 PHP
PHP输入输出流学习笔记
2015/05/12 PHP
thinkphp实现163、QQ邮箱收发邮件的方法
2015/12/18 PHP
基于PHP实现等比压缩图片大小
2016/03/04 PHP
php基于ob_start(ob_gzhandler)实现网页压缩功能的方法
2017/02/18 PHP
php上传excel表格并获取数据
2017/04/27 PHP
基于jQuery实现点击同时更改两个iframe的网址
2010/07/01 Javascript
jquery+css+ul模拟列表菜单具体实现思路
2013/04/15 Javascript
键盘上一张下一张兼容IE/google/firefox等浏览器
2014/01/28 Javascript
jQuery中bind(),live(),delegate(),on()绑定事件方法实例详解
2016/01/19 Javascript
AngularJS基础 ng-readonly 指令简单示例
2016/08/02 Javascript
jquery实现自适应banner焦点图
2017/02/16 Javascript
JavaScript实现设置默认日期范围为最近40天的方法分析
2017/07/12 Javascript
快速了解vue-cli 3.0 新特性
2018/02/28 Javascript
JavaScript中十种一步拷贝数组的方法实例详解
2019/04/22 Javascript
Bootstrap table 服务器端分页功能实现方法示例
2020/06/01 Javascript
arcgis.js控制地图地体的显示范围超出区域自动弹回(实现思路)
2021/01/28 Javascript
手动实现vue2.0的双向数据绑定原理详解
2021/02/06 Vue.js
[19:14]DOTA2 HEROS教学视频教你分分钟做大人-维萨吉
2014/06/24 DOTA
使用优化器来提升Python程序的执行效率的教程
2015/04/02 Python
Python中协程用法代码详解
2018/02/10 Python
Django使用Celery异步任务队列的使用
2018/03/13 Python
python pandas 组内排序、单组排序、标号的实例
2018/04/12 Python
详谈套接字中SO_REUSEPORT和SO_REUSEADDR的区别
2018/04/28 Python
Python读取Excel表格,并同时画折线图和柱状图的方法
2018/10/14 Python
Python文件操作中进行字符串替换的方法(保存到新文件/当前文件)
2019/06/28 Python
Python调用.NET库的方法步骤
2019/12/27 Python
django admin 添加自定义链接方式
2020/03/11 Python
NULL是什么,它是怎么定义的
2015/05/09 面试题
幼儿园实习生辞职信
2014/01/20 职场文书
2014年征兵标语
2014/06/20 职场文书
小学教师个人工作总结2015
2015/04/20 职场文书
初任公务员培训心得体会
2016/01/08 职场文书
代码解析React中setState同步和异步问题
2021/06/03 Javascript
Nginx stream 配置代理(Nginx TCP/UDP 负载均衡)
2021/11/17 Servers