简体   繁体   English

在 Python 中搜索模式

[英]Search for pattern in Python

I have a file in which I need for a pattern and I need to extract the values after the pattern and I should append it to a list.我有一个文件,我需要一个模式,我需要提取模式之后的值,我应该将它 append 到一个列表中。

Sample of file I am using:我正在使用的文件示例:

Container:container_12345

asfacaasda:..........
sdaasdasda:............
dasdadaadada.....

jiasjafjsdf.............
sdfsdfsd..................

Container:container_23456

dasdafsadf....
dfsdfsaf.....
fsfsfsdf......

I have tried to extract the values present after the pattern "Container:" (ie) container_12345我试图提取模式“容器:”(即)container_12345 之后存在的值

My code:我的代码:

List = []
pattern=re.compile("Container:")
fop=open(filename,"r")
for line in fop:
    for char in line:
        result=pattern.search(char)
        List.append(result.group(1))

print(List)

My Output:我的 Output:

List.append(result.group(1))
AttributeError: 'NoneType' object has no attribute 'group'

Output what I am expecting: Output 我所期待的:

['container_12345','container_23456']  #Present in that list

Please explain me what I have done wrong.请解释我做错了什么。 Thanks in advance!提前致谢!

Regular expressions are a useless complication if you are looking for a static string.如果您正在寻找 static 字符串,则正则表达式是无用的复杂功能。

for line in lines:
    if line.startswith("Container:"):
        print(line[len("Container:"):].strip())

The call to strip() trims any whitespace from the beginning or the end;strip()的调用从开头或结尾修剪任何空白; if you omit it, keep in mind that line ends with a newline character.如果您省略它,请记住该line以换行符结尾。

The actual error in your code is that you attempt to extract something even when search returns None ;代码中的实际错误是,即使search返回None ,您也尝试提取某些内容; also, your regex seems slightly wrong, and you needlessly iterate over each character in each line, so the regex will never match.此外,您的正则表达式似乎有点错误,并且您不必要地遍历每行中的每个字符,因此正则表达式永远不会匹配。

Just use the expression on the whole content instead of iterating over it line by line.只需在整个内容上使用表达式,而不是逐行迭代它。
The re module has builtin functionality exactly fort his purpose: re模块的内置功能正是为了他的目的:

import re
rx = re.compile('^Container:\s*(.+)', re.M)
with open(your_file) as fp:
    containers = [m.group(1) for m in rx.finditer(fp.read())]
    print(containers)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM