我试图在列表中查找具有“###### - ##”格式的所有内容
我认为我在下面的代码中有正确的想法,但它不打印任何东西。 我的列表中的某些值具有该格式,我认为它应该打印它。 你能告诉我什么是错的吗?
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 lineIn 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"更多推荐
发布评论