0
我想用正則表達式簡化這個函數。 樣品輸入可以是這個函數的正則表達式
text =' At&T, " < I am > , At&T so < < & & '
我的代碼:
def replaceentity(text):
import re
import uuid
from cgi import escape
invalid_chars_map = {'&':'&', '<':'<', '>': '>', '"': """}
replace_values = {'<':'<', '>':'>'}
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
no ...正如你可以在我的代碼的邏輯中看到,如果該值已經存在於字典中我想跳過那個詞,因爲它是,即不想取代&從那 –