24小时热门版块排行榜    

Znn3bq.jpeg
查看: 2645  |  回复: 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 的主题更新
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 300求调剂 +4 橙a777 2026-04-15 4/200 2026-04-15 20:05 by 学员JpLReM
[考研] 0854求调剂 +9 门路摸摸 2026-04-15 9/450 2026-04-15 19:54 by 学员JpLReM
[考研] 307中医考研调剂 +4 于以采蘩 2026-04-14 4/200 2026-04-15 19:14 by AN流800
[考研] 290调剂生物0860 +37 哇哈哈,。 2026-04-11 43/2150 2026-04-15 18:55 by zhuwenxu
[考研] 求调剂推荐 +6 小聂爱学习 2026-04-14 6/300 2026-04-15 17:26 by 1005715100
[考研] 297,工科调剂?河南农业大学本科 +11 河南农业大学-能 2026-04-14 11/550 2026-04-15 15:03 by 学员JpLReM
[考研] 273求调剂 +5 白居不易. 2026-04-09 7/350 2026-04-15 13:16 by 西北望—风沙
[考研] 071000生物学调剂求助 +18 zzzzwww 2026-04-09 21/1050 2026-04-14 15:39 by zs92450
[考研] 一志愿沪9,326求生物学调剂 +10 刘墨墨 2026-04-13 10/500 2026-04-14 15:16 by zs92450
[考研] 求调剂 +16 雪逢冬 2026-04-10 16/800 2026-04-14 14:27 by 逆水乘风
[考研] 考研英一数一338分 +9 长江大学东校区 2026-04-13 10/500 2026-04-14 00:41 by 王珺璞
[考研] 一志愿华南理工大学331分材料求调剂 +10 天下ww 2026-04-09 11/550 2026-04-13 23:25 by pies112
[考研] 0856专硕求调剂 希望是a区院校 +24 好好休息好不好 2026-04-09 27/1350 2026-04-13 22:22 by pies112
[考研] B区0809 ,数一英一,290 求调剂 +3 泠潍1111 2026-04-12 4/200 2026-04-13 20:35 by 学员JpLReM
[考研] +10 李多米lee. 2026-04-12 11/550 2026-04-12 22:58 by yuyin1233
[考研] 344 材料专业 求调剂211 无地域要求 +8 hualkop 2026-04-11 8/400 2026-04-12 22:24 by fqwang
[找工作] 山东高校教师考核超级无底线,员工过不下去啦 +4 qut2026 2026-04-09 9/450 2026-04-12 00:54 by qut2026
[考研] 22408调剂315分 +3 zhuangyan123 2026-04-09 3/150 2026-04-12 00:25 by 蓝云思雨
[考研] 265求调剂 +12 风说她早忘了 2026-04-10 13/650 2026-04-10 18:56 by chemisry
[考研] 已调剂 +18 柴郡猫_ 2026-04-09 19/950 2026-04-09 22:10 by 柴郡猫_
信息提示
请填处理意见