新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:Python如何實現(xiàn)excel篩選后生成新表
python如何實現(xiàn)Excel篩選后生成新表

xlrd對exce進行讀操作,xlwt對exce進行寫操作。
實現(xiàn)把excel中某些單元格的部分字符作為關鍵字,進行查找。話不多說直接上代碼。
# -*- coding: utf-8 -*-
import xlrd
import xlwt
def read_excel():
workbook = xlrd.open_workbook('tttttt.xlsx') #讀取源excel文件
jieguo = xlwt.Workbook(encoding="ascii") #生成excel
wsheet = jieguo.add_sheet('sheet name') #生成sheet
sheetnum=workbook.nsheets #獲取源文件sheet數(shù)目
y=0 #生成的excel的行計數(shù)
for m in range(0,sheetnum):
sheet = workbook.sheet_by_index(m) #讀取源excel文件第m個sheet的內容
nrowsnum=sheet.nrows #獲取該sheet的行數(shù)
for i in range(0,nrowsnum):
date=sheet.row(i) #獲取該sheet第i行的內容
for n in range(0,len(date)):
aaa=str(date[n]) #把該行第n個單元格轉化為字符串,目的是下一步的關鍵字比對
if aaa.find('關鍵字')>0: #進行關鍵字比對,包含關鍵字返回1,否則返回0
y=y+1
for j in range(len(date)):
wsheet.write(y,j,sheet.cell_value(i,j)) #該行包含關鍵字,則把它所有單元格依次寫入入新生成的excel的第y行
jieguo.save('jieguo.xls') #保存新生成的Excel
if __name__ == '__main__':
read_excel()因為關鍵字判斷是針對單元格的,所以,如果一行數(shù)據(jù)中有多個關鍵字,結果也會生成多行。
更多技術請關注Python視頻教程。
分享題目:創(chuàng)新互聯(lián)Python教程:Python如何實現(xiàn)excel篩選后生成新表
網(wǎng)頁網(wǎng)址:http://fisionsoft.com.cn/article/dposcid.html


咨詢
建站咨詢
