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的web框架中配置app的教程
Apr 30 Python
python列表操作之extend和append的区别实例分析
Jul 28 Python
Python查询IP地址归属完整代码
Jun 21 Python
利用Python如何生成便签图片详解
Jul 09 Python
python中的decorator的作用详解
Jul 26 Python
Python 通过requests实现腾讯新闻抓取爬虫的方法
Feb 22 Python
解决pyqt5中QToolButton无法使用的问题
Jun 21 Python
python笔记_将循环内容在一行输出的方法
Aug 08 Python
原生python实现knn分类算法
Oct 24 Python
解决springboot yml配置 logging.level 报错问题
Feb 21 Python
python 远程执行命令的详细代码
Feb 15 Python
python字符串的一些常见实用操作
Apr 06 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
php4的彩蛋
2006/10/09 PHP
php session安全问题分析
2011/06/24 PHP
PHP curl 或 file_get_contents 获取需要授权页面的方法
2017/05/05 PHP
php使用pthreads v3多线程实现抓取新浪新闻信息操作示例
2020/02/21 PHP
CL vs ForZe BO5 第五场 2.13
2021/03/10 DOTA
jQuery 获取URL参数的插件
2010/03/04 Javascript
浅析jQuery的链式调用之each函数
2010/12/03 Javascript
Javascript 中的 call 和 apply使用介绍
2012/02/22 Javascript
Javascript计算两个marker之间的距离(Google Map V3)
2013/04/26 Javascript
extjs render 用法介绍
2013/09/11 Javascript
JS 有趣的eval优化输入验证实例代码
2013/09/22 Javascript
Thinkphp模板没有解析直接原样输出的解决方法
2014/10/31 Javascript
jQuery Form 表单提交插件之formSerialize,fieldSerialize,fieldValue,resetForm,clearForm,clearFields的应用
2016/01/23 Javascript
JavaScript中数组添加值和访问值常见问题
2016/02/06 Javascript
jQuery如何获取动态添加的元素
2016/06/24 Javascript
JavaScript中的Reflect对象详解(ES6新特性)
2016/07/22 Javascript
JS 数字转换为大写金额的简单实例
2016/08/04 Javascript
深入理解JS DOM事件机制
2016/08/06 Javascript
jQuery禁用快捷键例如禁用F5刷新 禁用右键菜单等的简单实现
2016/08/31 Javascript
JavaScript实现审核流程状态的动态显示进度条
2017/03/15 Javascript
es6学习笔记之Async函数基本教程
2017/05/11 Javascript
Bootstrap 中data-[*] 属性的整理
2018/03/13 Javascript
手把手教你用Node.js爬虫爬取网站数据的方法
2018/07/05 Javascript
react-native滑动吸顶效果的实现过程
2019/06/03 Javascript
采用Psyco实现python执行速度提高到与编译语言一样的水平
2014/10/11 Python
设置python3为默认python的方法
2018/10/31 Python
Python二进制文件读取并转换为浮点数详解
2019/06/25 Python
45个非常奇妙的CSS3 特性应用示例
2012/01/01 HTML / CSS
美国潜水装备、水肺潜水和浮潜设备商店:Leisure Pro
2018/08/08 全球购物
戴尔荷兰官方网站:Dell荷兰
2020/10/04 全球购物
我的applet原先好好的, 一放到web server就会有问题,为什么?
2016/05/10 面试题
业务经理的岗位职责
2013/11/16 职场文书
保险专业大学生职业规划书
2014/03/03 职场文书
医院财务人员岗位职责
2015/04/14 职场文书
特种设备安全管理制度
2015/08/06 职场文书
Mysql Online DDL的使用详解
2021/05/20 MySQL