国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python使用xlrd實現檢索excel中某列含有指定字符串記錄的方法

2020-02-23 01:10:37
字體:
來源:轉載
供稿:網友

本文實例講述了python使用xlrd實現檢索excel中某列含有指定字符串記錄的方法。分享給大家供大家參考。具體分析如下:

這里利用xlrd,將excel中某列數據中,含有指定字符串的記錄取出,并生成用這個字符串命名的txt文件

import osimport xlrd,sys# input the excel fileFilename=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 filebk=xlrd.open_workbook(Filename)#get the sheets numbershxrange=range(bk.nsheets)print shxrange#get the sheets namefor x in shxrange:  p=bk.sheets()[x].name.encode('utf-8')  print "Sheets Number(%s): %s" %(x,p.decode('utf-8'))# input your sheets namesname=int(raw_input('choose the sheet number:'))try:  sh=bk.sheets()[sname]except:  print "no this sheet"  #return Nonenrows=sh.nrowsncols=sh.ncols# return the lines and col numberprint "line:%d col:%d" %(nrows,ncols)#input the check columncolumnnum=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 columntestin=raw_input('input the string:')#find the cols and save to a txtoutputfilename=testin + '.txt'outputfile=open(outputfilename,'w')#find the rows which you want to select and write to a txt filefor 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()

希望本文所述對大家的Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 山阳县| 金门县| 会理县| 乳山市| 汕尾市| 杭州市| 漳浦县| 榆社县| 阿克苏市| 宜城市| 稻城县| 定陶县| 平利县| 通海县| 康保县| 都江堰市| 宾阳县| 崇州市| 辉县市| 应用必备| 惠东县| 吉木乃县| 寿光市| 西林县| 旬阳县| 漳浦县| 长沙市| 东乡| 习水县| 璧山县| 博爱县| 临朐县| 松阳县| 凌源市| 垫江县| 永年县| 自治县| 静安区| 称多县| 隆德县| 洛川县|