2012-06-28 64 views
-4

我有一個我想要循環的列表(對trimtemp in trim)並查看它是否在給定字符串(modeltrim)中有任何匹配。代碼在大多數時間運行良好,但是,每隔不時會發生以下錯誤:使用re.search的不平衡括號錯誤python

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 

您的幫助將不勝感激!

+7

請同時發佈錯誤代碼。我們可以從跟蹤中挑選出相關的一行,但需要查看更多。 –

+6

不知道'trimtemp'中的內容,我們無法告訴你任何錯誤已經存在。這個錯誤似乎是自我解釋。 – Hamish

回答

2

此行似乎是問題

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

你應該看什麼format(trimtemp)包含。也許只需加上一個

print format(trimtemp) 

就行了。可能它包含()這對re有意義。也許re.escape(format(trimtemp))是你所需要的