24小时热门版块排行榜    

查看: 2637  |  回复: 0

mybag1

木虫 (正式写手)

[交流] python利用xlrd完成excel中某列检索含有指定字符串的记录(原代)

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()

我的空间:http://user.qzone.qq.com/384327409/blog/1326700667
回复此楼
http://www.cnblogs.com/xiaowuyi
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

智能机器人

Robot (super robot)

我们都爱小木虫

相关版块跳转 我要订阅楼主 mybag1 的主题更新
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 考研化学308分求调剂 +8 你好明天你好 2026-03-23 9/450 2026-03-25 09:13 by aa331100
[考研] 生物学学硕求调剂 +7 小羊睡着了? 2026-03-23 10/500 2026-03-25 02:24 by 清风拂扬。 m
[考研] 上海电力大学材料防护与新材料重点实验室招收调剂研究生(材料、化学、电化学,环境) +4 我爱学电池 2026-03-23 4/200 2026-03-25 00:59 by 1027_324
[考研] 311求调剂 +3 冬十三 2026-03-24 3/150 2026-03-24 21:31 by peike
[考研] 300求调剂,材料科学英一数二 +5 leaflight 2026-03-24 5/250 2026-03-24 16:25 by laoshidan
[考研] 【双一流院校新能源、环境材料,材料加工与模拟招收大量调剂】 +4 Higraduate 2026-03-22 7/350 2026-03-24 11:23 by 种大树
[考博] 26申博自荐 +3 whh869393 2026-03-24 3/150 2026-03-24 09:55 by 21018060
[考研] 化学308分求调剂 +3 你好明天你好 2026-03-23 3/150 2026-03-23 20:11 by macy2011
[考研] 一志愿陕师大生物学071000,298分,求调剂 +3 SYA! 2026-03-23 3/150 2026-03-23 19:09 by macy2011
[考研] 336求调剂 +4 收到VS 2026-03-20 4/200 2026-03-23 19:02 by macy2011
[考研] 316求调剂 +7 梁茜雯 2026-03-19 7/350 2026-03-23 16:21 by lingjue
[考研] 263求调剂 +6 yqdszhdap- 2026-03-22 9/450 2026-03-23 12:57 by yqdszhdap-
[考研] 求老师收我 +3 zzh16938784 2026-03-23 3/150 2026-03-23 12:56 by ztnimte
[考研] 293求调剂 +3 涛涛Wjt 2026-03-22 5/250 2026-03-22 22:21 by jiangpengfei
[考研] 材料与化工085600,总分304,本科有两篇sci参与,求调剂 +4 幸运的酱酱 2026-03-22 5/250 2026-03-22 20:15 by edmund7
[考研] 考研调剂 +4 来好运来来来 2026-03-21 4/200 2026-03-22 12:15 by 星空星月
[考研] 324分 085600材料化工求调剂 +4 llllkkkhh 2026-03-18 4/200 2026-03-21 01:24 by JourneyLucky
[考研] 308求调剂 +3 阿姐阿姐家啊 2026-03-18 3/150 2026-03-20 23:24 by JourneyLucky
[考研] 317求调剂 +5 申子申申 2026-03-19 9/450 2026-03-20 22:26 by JourneyLucky
[考研] 一志愿南理工085701环境302求调剂院校 +3 葵梓卫队 2026-03-20 3/150 2026-03-20 19:28 by zhukairuo
信息提示
请填处理意见