我有一个我想循环的列表(对于trim中的trimtemp),看看它是否在给定的字符串(modeltrim)中有任何匹配。代码在大多数情况下运行正常但是,它偶尔会出现以下错误:
Traceback (most recent call last):
File "/home/hostadl/PricesFinal.py", line 318, in <module>
main()
File "/home/hostadl/PricesFinal.py", line 215, in main
match = re.search(r'{0}'.format(trimtemp), modeltrim, re.IGNORECASE)
File "/usr/local/lib/python3.2/re.py", line 158, in search
return _compile(pattern, flags).search(string)
File "/usr/local/lib/python3.2/re.py", line 256, in _compile
return _compile_typed(type(pattern), pattern, flags)
File "/usr/local/lib/python3.2/functools.py", line 180, in wrapper
result = user_function(*args, **kwds)
File "/usr/local/lib/python3.2/re.py", line 268, in _compile_typed
return sre_compile.compile(pattern, flags)
File "/usr/local/lib/python3.2/sre_compile.py", line 491, in compile
p = sre_parse.parse(p, flags)
File "/usr/local/lib/python3.2/sre_parse.py", line 692, in parse
p = _parse_sub(source, pattern, 0)
File "/usr/local/lib/python3.2/sre_parse.py", line 315, in _parse_sub
itemsappend(_parse(source, state))
File "/usr/local/lib/python3.2/sre_parse.py", line 642, in _parse
raise error("unbalanced parenthesis")
sre_constants.error: unbalanced parenthesis
非常感谢您的帮助!
答案 0 :(得分:2)
此行似乎是问题
match = re.search(r'{0}'.format(trimtemp), modeltrim, re.IGNORECASE)
您应该查看format(trimtemp)
包含的内容。也许只需添加一个
print format(trimtemp)
之前就行了。可能它包含(
或)
,它们对re
具有重要意义。或许re.escape(format(trimtemp))
就是你所需要的