-4

我有一个要循环遍历的列表(用于 trimtemp 中的 trim),并查看它在给定字符串(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

您的帮助将不胜感激!

4

1 回答 1

2

这条线似乎是问题所在

match = re.search(r'{0}'.format(trimtemp), modeltrim, re.IGNORECASE)

你应该看看format(trimtemp)包含什么。也许添加只是添加一个

print format(trimtemp)

之前就行了。可能它包含()对 具有重要意义re。也许re.escape(format(trimtemp))是你需要的

于 2012-06-28T02:33:16.797 回答