python正则提取excel_Python:正则表达式操作Excel???

代码:

#_*_ coding:utf-8 _*_

#__author__=观海云不远

#__date__ = 2019-07-11

#读写excel

import xlwt

import xlrd

import re

workbook = xlrd.open_workbook(data.xlsx)

sheet = workbook.sheet_by_index(0)

data = []

for rx in range(0, sheet.nrows):

row = sheet.row(rx)

item = []

colIndex = 0

isMatch = False

for cx in row:

val = cx.value

#第1列,可根据需要更改

if colIndex == 0:

if bool(re.search(rd度, val)) and bool(re.search(剑南春, val)) and bool(re.search(rdml, val)):

#print(val)

isMatch = True

colIndex += 1

item.append(cx)

if not isMatch:

data.append(item)

#print(data)

workbook = xlwt.Workbook()

sheet = workbook.add_sheet(output)

rIndex = 0

for row in iter(data):

cIndex = 0

for cel in row:

sheet.write(rIndex, cIndex, cel.value)

cIndex += 1

rIndex += 1

workbook.save(data_output.xls)

代码: #_*_ coding:utf-8 _*_ #__author__=观海云不远 #__date__ = 2019-07-11 #读写excel import xlwt import xlrd import re workbook = xlrd.open_workbook(data.xlsx) sheet = workbook.sheet_by_index(0) data = [] for rx in range(0, sheet.nrows): row = sheet.row(rx) item = [] colIndex = 0 isMatch = False for cx in row: val = cx.value #第1列,可根据需要更改 if colIndex == 0: if bool(re.search(rd度, val)) and bool(re.search(剑南春, val)) and bool(re.search(rdml, val)): #print(val) isMatch = True colIndex += 1 item.append(cx) if not isMatch: data.append(item) #print(data) workbook = xlwt.Workbook() sheet = workbook.add_sheet(output) rIndex = 0 for row in iter(data): cIndex = 0 for cel in row: sheet.write(rIndex, cIndex, cel.value) cIndex += 1 rIndex += 1 workbook.save(data_output.xls)
经验分享 程序员 微信小程序 职场和发展