I pretty new to python, so i have a dictionary with some keys in it, and a string. I have to replace the string if a pattern found in the dictionary exists in the string. both the dictionary and string are very large. I'm using a regex to find the patterns.
It all works fine until a key like this pops up '-(' or this '(-)' in which case python gives an error for unbalanced parenthesis.
Here's how the code I've written looks:
somedict={'-(':'value1','(-)':'value2'}
somedata='this is some data containing -( and (-)'
for key in somedict.iterkeys():
somedata=re.sub(key, 'newvalue', somedata)
Here's the error I've got in the console
Traceback (most recent call last):
File "<console>", line 2, in <module>
File "C:\Python27\lib\re.py", line 151, in sub
return _compile(pattern, flags).sub(repl, string, count)
File "C:\Python27\lib\re.py", line 244, in _compile
raise error, v # invalid expression
error: unbalanced parenthesis
I've also tried it many ways using the regex compiler and searched a lot but didn't find anything addressing the problem. Any help is appreciated.