python使用xlrd實(shí)現(xiàn)檢索excel中某列含有指定字符串記錄的方法
本文實(shí)例講述了python使用xlrd實(shí)現(xiàn)檢索excel中某列含有指定字符串記錄的方法。分享給大家供大家參考。具體分析如下:
這里利用xlrd,將excel中某列數(shù)據(jù)中,含有指定字符串的記錄取出,并生成用這個(gè)字符串命名的txt文件
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()
希望本文所述對大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python如何將多個(gè)模型的ROC曲線繪制在一張圖(含圖例)
這篇文章主要給大家介紹了關(guān)于python如何將多個(gè)模型的ROC曲線繪制在一張圖的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02
Python學(xué)習(xí)之二叉樹實(shí)現(xiàn)的示例詳解
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)二叉樹的相關(guān)知識,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-04-04
python實(shí)現(xiàn)PolynomialFeatures多項(xiàng)式的方法
這篇文章主要介紹了python實(shí)現(xiàn)PolynomialFeatures多項(xiàng)式的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Python文件操作和數(shù)據(jù)格式詳解(簡單簡潔)
文本處理是腳本語言的強(qiáng)項(xiàng),下面這篇文章主要給大家介紹了關(guān)于Python文件操作和數(shù)據(jù)格式的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
一篇文章帶你深入學(xué)習(xí)Python函數(shù)
這篇文章主要帶大家深入學(xué)習(xí)Python函數(shù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-01-01
Python機(jī)器學(xué)習(xí)應(yīng)用之基于BP神經(jīng)網(wǎng)絡(luò)的預(yù)測篇詳解
BP(back?propagation)神經(jīng)網(wǎng)絡(luò)是1986年由Rumelhart和McClelland為首的科學(xué)家提出的概念,是一種按照誤差逆向傳播算法訓(xùn)練的多層前饋神經(jīng)網(wǎng)絡(luò),是應(yīng)用最廣泛的神經(jīng)網(wǎng)絡(luò)模型之一2022-01-01

