24小时热门版块排行榜    

查看: 2635  |  回复: 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 的主题更新
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 一志愿北京化工大学 070300 学硕 336分 求调剂 +6 vv迷 2026-03-22 6/300 2026-03-23 22:00 by fuyu_
[考研] 环境学硕288求调剂 +7 皮皮皮123456 2026-03-22 7/350 2026-03-23 20:47 by baobaoye
[考研] 085600材料与化工调剂 +6 A-哆啦Z梦 2026-03-23 11/550 2026-03-23 19:57 by pswait
[考研] 333求调剂 +3 ALULU4408 2026-03-23 3/150 2026-03-23 19:04 by macy2011
[考研] 276求调剂。有半年电池和半年高分子实习经历 +8 材料学257求调剂 2026-03-23 9/450 2026-03-23 13:01 by ztnimte
[考研] 289材料与化工(085600)B区求调剂 +3 这么名字咋样 2026-03-22 4/200 2026-03-22 17:56 by 云民大李老师
[考研] 考研调剂 +4 来好运来来来 2026-03-21 4/200 2026-03-22 12:15 by 星空星月
[考研] 材料求调剂 +5 @taotao 2026-03-21 5/250 2026-03-21 20:55 by lbsjt
[考研] 0703化学调剂 +4 妮妮ninicgb 2026-03-21 4/200 2026-03-21 18:39 by 学员8dgXkO
[考研] 278求调剂 +9 烟火先于春 2026-03-17 9/450 2026-03-21 17:47 by 学员8dgXkO
[基金申请] 学校已经提交到NSFC,还能修改吗? 40+4 babangida 2026-03-19 9/450 2026-03-21 16:12 by babangida
[考研] 22 350 本科985求调剂,求老登收留 +3 李轶男003 2026-03-20 3/150 2026-03-21 13:28 by 搏击518
[考研] 265求调剂 +3 Jack?k?y 2026-03-17 3/150 2026-03-21 03:17 by JourneyLucky
[考研] 085700资源与环境308求调剂 +12 墨墨漠 2026-03-18 13/650 2026-03-21 01:42 by JourneyLucky
[考研] 22408 344分 求调剂 一志愿 华电计算机技术 +4 solanXXX 2026-03-20 4/200 2026-03-20 23:49 by alg094825
[考研] 材料与化工 322求调剂 +4 然11 2026-03-19 4/200 2026-03-20 22:12 by luoyongfeng
[考研] 295材料求调剂,一志愿武汉理工085601专硕 +5 Charlieyq 2026-03-19 5/250 2026-03-20 20:35 by JourneyLucky
[考研] 求调剂 +3 eation27 2026-03-20 3/150 2026-03-20 19:32 by JourneyLucky
[考研] 广西大学家禽遗传育种课题组2026年硕士招生(接收计算机专业调剂) +3 123阿标 2026-03-17 3/150 2026-03-20 15:58 by 飞行琦
[考研] 考研调剂 +3 淇ya_~ 2026-03-17 5/250 2026-03-17 09:25 by Winj1e
信息提示
请填处理意见