国产探花免费观看_亚洲丰满少妇自慰呻吟_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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永和县| 鹤岗市| 铁岭县| 千阳县| 阿巴嘎旗| 罗甸县| 信阳市| 达尔| 东山县| 新兴县| 大埔县| 稷山县| 汝州市| 兴国县| 胶南市| 南皮县| 周至县| 哈尔滨市| 丹东市| 马鞍山市| 涞水县| 米林县| 余江县| 莒南县| 邻水| 甘肃省| 平安县| 焉耆| 类乌齐县| 南皮县| 大荔县| 阜南县| 南平市| 衡阳市| 怀宁县| 独山县| 昭苏县| 山西省| 虹口区| 左权县| 油尖旺区|