Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中搜索模式_Python - Fatal编程技术网

在Python中搜索模式

在Python中搜索模式,python,Python,我有一个文件,其中我需要一个模式,我需要提取模式后的值,我应该将它附加到一个列表中 我正在使用的文件示例: Container:container_12345 asfacaasda:.......... sdaasdasda:............ dasdadaadada..... jiasjafjsdf............. sdfsdfsd.................. Container:container_23456 dasdafsadf.... dfsdfsaf.

我有一个文件,其中我需要一个模式,我需要提取模式后的值,我应该将它附加到一个列表中

我正在使用的文件示例:

Container:container_12345

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

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

Container:container_23456

dasdafsadf....
dfsdfsaf.....
fsfsfsdf......
我试图提取模式“Container:(即)Container_12345)之后的值

我的代码:

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)
我的输出:

List.append(result.group(1))
AttributeError: 'NoneType' object has no attribute 'group'
输出我期望的内容:

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

请解释我做错了什么。提前谢谢

只需在整个内容上使用表达式,而不是逐行迭代。
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)

只需在整个内容上使用表达式,而不是逐行迭代。
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)

如果要查找静态字符串,则正则表达式是一个无用的复杂因素

对于行中的行:
如果line.startswith(“容器:”):
打印(行[len(“容器:):].strip())
调用
strip();如果省略它,请记住,
line
以换行符结尾


代码中的实际错误是,即使
search
返回
None
,您也试图提取某些内容;此外,您的正则表达式似乎有点错误,并且您不必要对每行中的每个字符进行迭代,因此正则表达式将永远不会匹配。

如果您正在查找静态字符串,则正则表达式是一个无用的复杂问题

对于行中的行:
如果line.startswith(“容器:”):
打印(行[len(“容器:):].strip())
调用
strip();如果省略它,请记住,
line
以换行符结尾


代码中的实际错误是,即使
search
返回
None
,您也试图提取某些内容;此外,您的正则表达式似乎有点错误,并且您不必要对每行中的每个字符进行迭代,因此正则表达式将永远不会匹配。

嗯,您的正则表达式模式中没有定义任何组。[而且您正在将该模式应用于单行字符。您的正则表达式模式中没有定义任何组。[而且您正在将模式应用于单行字符。非常感谢!但只有当我删除+1时,我才能获得输出。+1删除容器的第一个字母并生成容器12345。哦,抱歉;修复了。非常感谢!但只有当我删除+1时,我才能获得输出。+1删除容器的第一个字母并重新生成Container_12345中的苏丹。哦,抱歉;修复。非常感谢您的支持!非常感谢您的支持!