python使用xlrd实现检索excel中某列含有指定字符串记录的方法

 更新时间:2015年05月09日 16:21:41   作者:小五义  
这篇文章主要介绍了python使用xlrd实现检索excel中某列含有指定字符串记录的方法,涉及Python使用xlrd模块检索Excel的技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了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 最简单的实现适配器设计模式的示例

    这篇文章主要介绍了python 最简单的实现适配器设计模式的示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • 利用pyshp包给shapefile文件添加字段的实例

    利用pyshp包给shapefile文件添加字段的实例

    今天小编就为大家分享一篇利用pyshp包给shapefile文件添加字段的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12
  • python格式化字符串实例总结

    python格式化字符串实例总结

    这篇文章主要介绍了python格式化字符串的方法,实例展示了常见的几类Python针对字符串的格式方法,非常实用,需要的朋友可以参考下
    2014-09-09
  • Python Scala中使用def语句定义方法的详细过程

    Python Scala中使用def语句定义方法的详细过程

    这篇文章主要介绍了Python Scala中使用def语句定义方法,Scala的方法是类的一部分,而函数是一个对象可以赋值给一个变量,下面来讲解Scala的方法,需要的朋友可以参考下
    2022-09-09
  • 基于Python实现抢注大词的提词工具

    基于Python实现抢注大词的提词工具

    这篇文章主要为大家详细介绍了如何利用Python语言实现抢注大词的提词工具,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-02-02
  • python scrapy框架的日志文件问题

    python scrapy框架的日志文件问题

    这篇文章主要介绍了python scrapy框架的日志文件问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • pyCharm中python对象的自动提示方式

    pyCharm中python对象的自动提示方式

    这篇文章主要介绍了pyCharm中python对象的自动提示方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • 浅谈五大Python Web框架

    浅谈五大Python Web框架

    Python这么多框架,能挨个玩个遍的人不多,坦白的说我也只用过其中的三个开发过项目,另外一些稍微接触过,所以这里只能浅谈一下,欢迎懂行的朋友们补充
    2017-03-03
  • Python2.7实现多进程下开发多线程示例

    Python2.7实现多进程下开发多线程示例

    这篇文章主要为大家详细介绍了Python2.7实现多进程下开发多线程示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-05-05
  • 详解Pandas的三大利器(map,apply,applymap)

    详解Pandas的三大利器(map,apply,applymap)

    这篇文章主要为大家介绍了pandas中的三大利器: map、apply、applymap,他们经常在进行数据处理的时候用到,需要的可以参考一下
    2022-02-02

最新评论