• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python使用xlrd实现检索excel中某列含有指定字符串记录的方法

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

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python xlrd,python安装xlrd模块,python xlrd安装,python3 xlrd,python xlrd用法等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了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()
</div>

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • 详解python中xlrd包的安装与处理Excel表格
  • Python使用xlrd模块操作Excel数据导入的方法
  • Python使用xlrd模块操作Excel数据导入的方法
  • python使用xlrd实现检索excel中某列含有指定字符串记录的方法
  • Python中使用第三方库xlrd来写入Excel文件示例
  • Python中使用第三方库xlrd来读取Excel示例
  • Python使用xlrd读取Excel格式文件的方法

相关文章

  • Python图算法实例分析
  • 使用Python中PDB模块中的命令来调试Python代码的教程
  • Python的面向对象编程方式学习笔记
  • Python多线程经典问题之乘客做公交车算法实例
  • Python数组遍历的简单实现方法小结
  • PYTHON正则表达式 re模块使用说明
  • Python解析xml中dom元素的方法
  • 使用 Python 获取 Linux 系统信息的代码
  • python3实现ftp服务功能(客户端)
  • python中常用的九种预处理方法分享

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • pymongo实现控制mongodb中数字字段做加法的方法
    • python使用7z解压apk包的方法
    • Python中计算三角函数之cos()方法的使用简介
    • python字符串str和字节数组相互转化方法
    • Python中__new__与__init__方法的区别详解
    • Python编程生成随机用户名及密码的方法示例
    • python实现跨文件全局变量的方法
    • Django的session中对于用户验证的支持
    • python基础教程之popen函数操作其它程序的输入和输出示例
    • 使用Python编写一个模仿CPU工作的程序

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有