我必须在文件中搜索以下模式,(任何匹配都符合条件)
pattern_strings = ['\xc2d', '\xa0', '\xe7', '\xc3\ufffdd', '\xc2\xa0', '\xc3\xa7', '\xa0\xa0', '\xc2', '\xe9']
pattern = [re.compile(x) for x in pattern_strings]
并使用此功能
def find_pattern(path):
with open(path, 'r') as f:
for line in f:
found = pattern.search(line)
if found:
logging.info('found - ' + found)
当我尝试使用它时
find_pattern('myfile')
我懂了AttributeError: "'list' object has no attribute 'search'"
因为模式是
[<_sre.SRE_Pattern object at 0x107948378>, <_sre.SRE_Pattern object at 0x107b31c70>, <_sre.SRE_Pattern object at 0x107b31ce0>, <_sre.SRE_Pattern object at 0x107ac3cb0>, <_sre.SRE_Pattern object at 0x107b747b0>, <_sre.SRE_Pattern object at 0x107b74828>, <_sre.SRE_Pattern object at 0x107b748a0>, <_sre.SRE_Pattern object at 0x107b31d50>, <_sre.SRE_Pattern object at 0x107b31dc0>]
我怎样才能有一种模式来查找所有字符串pattern_strings
?