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进行数据提取的方法总结
Aug 22 Python
Python利用Beautiful Soup模块创建对象详解
Mar 27 Python
python Opencv将图片转为字符画
Feb 19 Python
pycharm恢复默认设置或者是替换pycharm的解释器实例
Oct 29 Python
解决webdriver.Chrome()报错:Message:'chromedriver' executable needs to be in Path
Jun 12 Python
用vue.js组件模拟v-model指令实例方法
Jul 05 Python
Python在OpenCV里实现极坐标变换功能
Sep 02 Python
python实现xlwt xlrd 指定条件给excel行添加颜色
Jul 14 Python
python 制作简单的音乐播放器
Nov 25 Python
五种Python转义表示法
Nov 27 Python
Python 爬虫批量爬取网页图片保存到本地的实现代码
Dec 24 Python
python 将html转换为pdf的几种方法
Dec 29 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
收音机另类DIY - 纸巾盒做外壳
2021/03/02 无线电
php实现按指定大小等比缩放生成上传图片缩略图的方法
2014/12/15 PHP
php判断邮箱地址是否存在的方法
2016/02/13 PHP
ThinkPHP实现附件上传功能
2017/04/27 PHP
PHP基于DOMDocument解析和生成xml的方法分析
2017/07/17 PHP
Yii2 队列 shmilyzxt/yii2-queue 简单概述
2017/08/02 PHP
JS 添加网页桌面快捷方式的代码详细整理
2012/12/27 Javascript
jquery div拖动效果示例代码
2013/12/08 Javascript
使用JavaScript实现alert的实例代码
2017/07/06 Javascript
JavaScript 基础表单验证示例(纯Js实现)
2017/07/20 Javascript
基于Vue实现图书管理功能
2017/10/17 Javascript
微信小程序自定义可滑动日历界面
2018/12/28 Javascript
浅谈对于react-thunk中间件的简单理解
2019/05/01 Javascript
如何使用Jquery动态生成二级选项列表
2020/02/06 jQuery
部署vue+Springboot前后端分离项目的步骤实现
2020/05/31 Javascript
详解如何在Javascript中使用Object.freeze()
2020/10/18 Javascript
[03:55]2014DOTA2国际邀请赛 Fnatic经理采访赢DK在情理之中
2014/07/10 DOTA
[52:00]2018DOTA2亚洲邀请赛 4.1 小组赛 A组加赛 LGD vs Optic
2018/04/02 DOTA
在mac下查找python包存放路径site-packages的实现方法
2018/11/06 Python
Django中create和save方法的不同
2019/08/13 Python
解决Atom安装Hydrogen无法运行python3的问题
2019/08/28 Python
python如何快速拼接字符串
2020/10/28 Python
雅诗兰黛旗下走天然植物路线的彩妆品牌:Prescriptives
2016/08/14 全球购物
国际象棋商店:The Chess Store
2018/07/09 全球购物
优秀求职自荐信怎样写
2013/12/18 职场文书
党员个人思想汇报
2013/12/28 职场文书
药品促销活动方案
2014/02/14 职场文书
2014年“世界无车日”活动方案
2014/09/21 职场文书
2015年新学期寄语
2015/02/26 职场文书
项目投资意向书范本
2015/05/09 职场文书
运动会加油稿50字
2015/07/21 职场文书
公司中层管理培训心得体会
2016/01/11 职场文书
python实现ROA算子边缘检测算法
2021/04/05 Python
详解MindSpore自定义模型损失函数
2021/06/30 Python
python opencv将多个图放在一个窗口的实例详解
2022/02/28 Python
小程序实现侧滑删除功能
2022/06/25 Javascript