欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)提取Excel指定關(guān)鍵詞的行數(shù)據(jù)

 更新時(shí)間:2022年03月07日 11:06:54   作者:用余生去守護(hù)  
這篇文章主要為大家介紹了如何利用Python實(shí)現(xiàn)提取Excel指定關(guān)鍵詞的行數(shù)據(jù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起動(dòng)手試一試

一、需求描述

1.圖片展示

從如圖所示的數(shù)據(jù)中提取含有"python"、"ubuntu"關(guān)鍵詞的所有行數(shù)據(jù),其它的不提?。?/p>

備注: 關(guān)鍵詞和數(shù)據(jù)行列數(shù)可自定義!??!

提取前:

提取后:

2.提取方法

代碼如下(示例):

import xlrd
import xlwt

data = xlrd.open_workbook(r'shuju.xlsx')
rtable = data.sheets()[0]
wbook = xlwt.Workbook(encoding='utf-8',style_compression = 0)
wtable = wbook.add_sheet('sheet1',cell_overwrite_ok = True)

count = 0
keyword = ('python')
keyword1 = ('ubuntu')  #可添加多個(gè)關(guān)鍵詞
for i in range(0,40):  #區(qū)域按數(shù)據(jù)包含的行數(shù)進(jìn)行填寫,過多會(huì)顯示超出范圍(out of range)
    if rtable.cell(i,2).value == keyword or rtable.cell(i,3).value == keyword or rtable.cell(i,4).value == keyword or rtable.cell(i,5).value == keyword or rtable.cell(i,2).value == keyword1 or rtable.cell(i,3).value == keyword1 or rtable.cell(i,4).value == keyword1 or rtable.cell(i,5).value == keyword1:
        for j in range(0,5):
            wtable.write(i,j,rtable.row_values(i)[j])
        count += 1
print (count)
wbook.save(r'medicaldata.xls')

缺點(diǎn):需要手動(dòng)刪除空白,容易出現(xiàn)超出范圍錯(cuò)誤??!

二、python提取第二版

1.圖片展示

提取前:

提取后:

2.提取方法

代碼如下(示例):

import os
import xlwt
import xlrd
from openpyxl import load_workbook

##目的文件夾
dirpath=r'E:\py\python3.7\test\test89tiqu'
keyword='python'

##遍歷函數(shù)
def files(dirpath, suffix=['.xls', 'xlsx']):
    for root ,dirs ,files in os.walk(dirpath):
        for name in files:
            if name.split('.')[-1] in suffix:
                yield os.path.join(root, name)

if __name__ == '__main__':

    jieguo = xlwt.Workbook(encoding="ascii")  #生成excel
    wsheet = jieguo.add_sheet('sheet name') #生成sheet    
    y=0 #生成的excel的行計(jì)數(shù)
    try:
        file_list = files(dirpath)
        for filename in file_list:
            workbook = xlrd.open_workbook(filename) #讀取源excel文件
            print(filename)
            sheetnum=workbook.nsheets  #獲取源文件sheet數(shù)目
            for m in range(0,sheetnum):
                sheet = workbook.sheet_by_index(m) #讀取源excel文件第m個(gè)sheet的內(nèi)容
                nrowsnum=sheet.nrows  #獲取該sheet的行數(shù)
                for i in range(0,nrowsnum):
                    date=sheet.row(i) #獲取該sheet第i行的內(nèi)容
                    for n in range(0,len(date)):
                        aaa=str(date[n]) #把該行第n個(gè)單元格轉(zhuǎn)化為字符串,目的是下一步的關(guān)鍵字比對(duì)
                        print(aaa)
                        if aaa.find(keyword)>0: #進(jìn)行關(guān)鍵字比對(duì),包含關(guān)鍵字返回1,否則返回0
                            y=y+1
                            for j in range(len(date)):
                                wsheet.write(y,j,sheet.cell_value(i,j)) #該行包含關(guān)鍵字,則把它所有單元格依次寫入入新生成的excel的第y行
        jieguo.save('jieguo.xls') #保存新生成的Excel
    except Exception as e:
        print(e)
                     
    jieguo.save('jieguo.xls') #保存新生成的Excel        

到此這篇關(guān)于Python實(shí)現(xiàn)提取Excel指定關(guān)鍵詞的行數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Python提取Excel行數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論