求助!!!现在有个需求,Python没怎么接触过
首先文档是这样,
想要用Python正则表达式和循环来迭代判断文档。A:判断名称中是否包含度数。B:判断商品名称是否包含品牌名称。C:判断名称是否包含毫升。A=》B=》C,把不满足条件的筛选出来成一个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(r'\d度', val)) and bool(re.search('剑南春', val)) and bool(re.search(r'\dml', 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')
详细原理可看:https://www.cnblogs.com/lurenjiashuo/p/python-read-write-excel-regex.html