查找列表中与特定格式匹配的所有项目(Find all items in a list that match a specific format)

编程入门 行业动态 更新时间:2024-10-28 03:18:45
查找列表中与特定格式匹配的所有项目(Find all items in a list that match a specific format)

我试图在列表中查找具有“###### - ##”格式的所有内容

我认为我在下面的代码中有正确的想法,但它不打印任何东西。 我的列表中的某些值具有该格式,我认为它应该打印它。 你能告诉我什么是错的吗?

for line in list_nums: if (line[-1:].isdigit()): if (line[-2:-1].isdigit()): if (line[-6:-5].isdigit()): if ("-" in line[-3:-2]): print(list_nums)

我的列表中的值包括123456-56和123456-98-98等格式,这就是我上面做的原因。 它是从excel表中提取的。

这是我更新的代码。

import xlrd from re import compile, match file_location = "R:/emily/emilylistnum.xlsx" workbook = xlrd.open_workbook(file_location) sheet = workbook.sheet_by_index(0) regexp = compile(r'^\d{d}-\d{2}$') list_nums = "" for row in range(sheet.nrows): cell = sheet.cell_value(row,0) if regexp.match(cell): list_nums += cell + "\n" print(list_nums)

我的Excel工作表包括: 581094-001 581095-001 581096-001 581097-01 5586987-007 SMX53-5567-53BP 552392-01-01 552392-02 552392-03-01 552392-10-01 552392-10-01 580062 580063 580065 580065 580066 543921-01 556664-55

(在一列中的每个单元格中)

I am trying to find everything in a list that has an format like "######-##"

I thought I had the right idea in my following code, but it isn't printing anything. Some values in my list have that format, and I would think it should print it. Could you tell me what's wrong?

for line in list_nums: if (line[-1:].isdigit()): if (line[-2:-1].isdigit()): if (line[-6:-5].isdigit()): if ("-" in line[-3:-2]): print(list_nums)

The values in my list consist of formats like 123456-56 and 123456-98-98, which is why what I did above. It is pulled from an excel sheet.

This is my updated code.

import xlrd from re import compile, match file_location = "R:/emily/emilylistnum.xlsx" workbook = xlrd.open_workbook(file_location) sheet = workbook.sheet_by_index(0) regexp = compile(r'^\d{d}-\d{2}$') list_nums = "" for row in range(sheet.nrows): cell = sheet.cell_value(row,0) if regexp.match(cell): list_nums += cell + "\n" print(list_nums)

my excel sheet consists of: 581094-001 581095-001 581096-001 581097-01 5586987-007 SMX53-5567-53BP 552392-01-01 552392-02 552392-03-01 552392-10-01 552392-10-01 580062 580063 580065 580065 580066 543921-01 556664-55

(in each cell down in one column)

最满意答案

如果只需匹配模式######-## (其中#是数字):

>>> from re import compile, match >>> regexp = compile(r'^\d{6}-\d{2}$') >>> print([line for line in list_nums if regexp.match(line)]) ['132456-78']

说明

您可以将模式compile为regexp对象,以便在匹配时更高效。 正则表达式是^\d{6}-\d{2}$其中:

^ # start of the line \d{6}-\d{2} # 6 digits, one dot then 2 digits $ # end of the line

在正则表达式中, \d表示数字(0到9之间的整数), {6}表示6次。 所以\d{3}表示3位数。 您应该阅读有关regexp的Python文档。


完整代码

基于您的评论的示例:

file_location = 'file.xlsx' workbook = xlrd.open_workbook(file_location) sheet = workbook.sheet_by_index(0) regexp = compile(r'^\d{6}-\d{2}$') list_nums = '' for row in range(sheet.nrows): cell = sheet.cell_value(row, 0) if regexp.match(cell): list_nums += cell + "\n"

If you need to only match the pattern ######-## (where # is a digit):

>>> from re import compile, match >>> regexp = compile(r'^\d{6}-\d{2}$') >>> print([line for line in list_nums if regexp.match(line)]) ['132456-78']

Explanations

You compile the pattern into a regexp object to be more efficient when matching. The regexp is ^\d{6}-\d{2}$ where:

^ # start of the line \d{6}-\d{2} # 6 digits, one dot then 2 digits $ # end of the line

In the regexp, \d means digit (an integer from 0 to 9) and {6} means 6 times. So \d{3} means 3 digits. You should read the Python documentation about regexps.


Full code

An example based on your comment:

file_location = 'file.xlsx' workbook = xlrd.open_workbook(file_location) sheet = workbook.sheet_by_index(0) regexp = compile(r'^\d{6}-\d{2}$') list_nums = '' for row in range(sheet.nrows): cell = sheet.cell_value(row, 0) if regexp.match(cell): list_nums += cell + "\n"

更多推荐

本文发布于:2023-07-26 05:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1271329.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中与   格式   项目   列表   Find

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!