I have a dictionary:
d = {'<word>':1,'-word':12, 'word':1, '$(*#%&^#&@#':2, '!@**$12word*&':4, '::':10, '1230324':1, '+635':5}
I want to remove only the entries which are all non-alphabet/non-digit characters, i.e. ,
.
?
!
:
;
and so on.
I've tried the following
regex = re.compile('[\!\?\.\,\:\;\*\(\)\-\+\<\>]')
regex = re.compile('a-zA-Z0-9_')
regex = re.compile('\\W')
regex = re.compile('[\W_]+') // from [1]
but they won't return my desired result, which is:
new_dict = {'<word>':1,'-word':12, 'word':1, '!@**$word*&':4, '1230324':1, '+635':5}
in which entries '$(*#%&^#&@#'
and ::
are removed.
Also, I use this code to remove the entries, in case it helps:
new_dict = {k:dictionary[k] for k in dictionary if re.match(regex, k)}
[1] Stripping everything but alphanumeric chars from a string in Python