2016-04-07 53 views
0

我想用正則表達式簡化這個函數。 樣品輸入可以是這個函數的正則表達式

text =' At&T, " < I am > , At&T so &#60; &lt; & & ' 

我的代碼:

def replaceentity(text): 
    import re 
    import uuid 
    from cgi import escape 
    invalid_chars_map = {'&':'&#38;', '<':'&#60;', '>': '&#62;', '"': "&#34;"} 
    replace_values = {'&lt;':'&#60;', '&gt;':'&#62;'} 
    replaced_dict = {} 
    for key, value in replace_values.items(): 
     text = text.replace(key, value) 
    print "after replace >>>>>> " + text 
    for word in text.split(): 
     if word in invalid_chars_map.values(): 
      print word 
      uid = str(uuid.uuid4()) 
      text = text.replace(word, uid) 
      replaced_dict[uid] = word 
    text = escape(text) 
    for i in replaced_dict.keys(): 
     text = text.replace(i, replaced_dict[i]) 
    print text 

回答

0

這是你想要的嗎?

>>> from cgi import escape 
>>> escaped = escape("""'At&T, " < I am > , At&T so &#60; &lt """) 
>>> escaped 
'\'At&amp;T, " &lt; I am &gt; , At&amp;T so &amp;#60; &amp;lt ' 
+0

no ...正如你可以在我的代碼的邏輯中看到,如果該值已經存在於字典中我想跳過那個詞,因爲它是,即不想取代&從那 –