python对excel筛选提取文本中数字_Python,从Excel列中提取数字并作为outpu写入

试图从Excel文件中的列中提取数字,并将其写入下一列。在

匹配条件:长度为5的任意数量,要么以“PB”开头,要么不以“PB”开头

我将匹配的数字长度限制为5,但是提取了一个“16”(第2行,D列)

f58e05ac3fcdee0f609a0273c0c8ea94.png

我怎样才能改进它?谢谢您。在import xlwt, xlrd, re

from xlutils.copy import copy

workbook = xlrd.open_workbook("C:\\Documents\\num.xlsx")

old_sheet = workbook.sheet_by_name("Sheet1")

wb = copy(workbook)

sheet = wb.get_sheet(0)

number_of_ships = old_sheet.nrows

for row_index in range(0, old_sheet.nrows):

Column_a = old_sheet.cell(row_index, 0).value

Column_b = old_sheet.cell(row_index, 1).value

a_b = Column_a + Column_b

found_PB = re.findall(r"[PB]+(\d{5})", a_b, re.I)

list_of_numbers = re.findall(r'\d+', a_b)

for f in found_PB:

if len(f) == 5:

sheet.write(row_index, 2, "";"".join(found_PB))

for l in list_of_numbers:

if len(l) == 5:

sheet.write(row_index, 3, "";"".join(list_of_numbers))

wb.save("C:\\Documents\\num-1.xls")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值