24小时热门版块排行榜    

查看: 2619  |  回复: 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 的主题更新
普通表情 高级回复 (可上传附件)
信息提示
请填处理意见