[英]Replace special characters from list in python
如何使用給定功能替換特殊字符(表情符號)。
例如
emoticons = \
[ ('__EMOT_SMILEY', [':-)', ':)', '(:', '(-:', ] ) ,\
('__EMOT_LAUGH', [':-D', ':D', 'X-D', 'XD', 'xD', ] ) ,\
('__EMOT_LOVE', ['<3', ':\*', ] ) ,\
('__EMOT_WINK', [';-)', ';)', ';-D', ';D', '(;', '(-;', ] ) ,\
('__EMOT_FROWN', [':-(', ':(', ] ) ,\
('__EMOT_CRY', [':,(', ':\'(', ':"(', ':(('] ) ,\
]
msg = 'I had a beautiful day :)'
期望的輸出
>> I had a beautiful day __EMOT_SMILEY
我知道如何使用字典來實現,但是在這里我有與每個功能關聯的多個值
以下代碼在這種情況下不起作用
for emote, replacement in emoticons.items():
msg = msg.replace(emote, replacement)
您可以使用字典和 正則表達式 :
import re
def replace(msg, emoticons):
d = {r: emote for emote, replacement in emoticons for r in replacement}
pattern = "|".join(map(re.escape, d))
msg = re.sub(pattern, lambda match: d[match.group()], msg)
return msg
print(replace(msg, emoticons)) # I had a beautiful day __EMOT_SMILEY
這應該做到:
emoticons = [ ('__EMOT_SMILEY', [':-)', ':)', '(:', '(-:', ] ),
('__EMOT_LAUGH', [':-D', ':D', 'X-D', 'XD', 'xD', ] ),
('__EMOT_LOVE', ['<3', ':\*', ] ),
('__EMOT_WINK', [';-)', ';)', ';-D', ';D', '(;', '(-;', ] ),
('__EMOT_FROWN', [':-(', ':(', '(:', '(-:', ] ),
('__EMOT_CRY', [':,(', ':\'(', ':"(', ':(('] )
]
emoticons = dict(emoticons)
emoticons = {v: k for k in emoticons for v in emoticons[k]}
msg = 'I had a beautiful day :)'
for item in emoticons:
if item in msg:
msg = msg.replace(item, emoticons[item])
因此,您創建了一個字典,將其反轉並替換了句子中存在的所有圖釋。
嘗試以下方法:
emoticons = [
('__EMOT_SMILEY', [':-)', ':)', '(:', '(-:',]),
('__EMOT_LAUGH', [':-D', ':D', 'X-D', 'XD', 'xD',]),
('__EMOT_LOVE', ['<3', ':\*',]),
('__EMOT_WINK', [';-)', ';) ', ';-D', ';D', '(;', '(-;',]),
('__EMOT_FROWN', [':-(', ':(', '(:', '(-:',]),
('__EMOT_CRY', [':,(', ':\'(', ':"(', ':((',]),
]
msg = 'I had a beautiful day :)'
for key, replaceables in dict(emoticons).items():
for replaceable in replaceables:
msg = msg.replace(replaceable, key)
print(msg)
>>> I had a beautiful day __EMOT_SMILEY
emoticons = [ ('__EMOT_SMILEY', [':-)', ':)', '(:', '(-:', ] ) ,
('__EMOT_LAUGH', [':-D', ':D', 'X-D', 'XD', 'xD', ] ) ,
('__EMOT_LOVE', ['<3', ':\*', ] ) ,
('__EMOT_WINK', [';-)', ';)', ';-D', ';D', '(;', '(-;', ] ) ,
('__EMOT_FROWN', [':-(', ':(', '(:', '(-:', ] ) ,
('__EMOT_CRY', [':,(', ':\'(', ':"(', ':(('] ) ,
]
msg = 'I had a beautiful day :)'
for emote, replacement in emoticons:
for symbol in replacement:
msg = msg.replace(symbol,emote)
print(msg)
這個怎么樣:
emoticons = [('__EMOT_SMILEY', [':-)', ':)', '(:', '(-:']),
('__EMOT_LAUGH', [':-D', ':D', 'X-D', 'XD', 'xD']),
('__EMOT_LOVE', ['<3', ':\*']),
('__EMOT_WINK', [';-)', ';)', ';-D', ';D', '(;', '(-;']),
('__EMOT_FROWN', [':-(', ':(', '(:', '(-:']),
('__EMOT_CRY', [':,(', ':\'(', ':"(', ':(('])]
msg = 'I had a beautiful day :)'
grabs = set([x for _, y in emoticons for x in y[1]])
for word in [x for x in msg.split() if all(y in grabs for y in x)]:
for emot_code, search_patterns in emoticons:
if word in search_patterns:
msg = msg.replace(word, emot_code)
print(msg) # I had a beautiful day __EMOT_SMILEY
它沒有嘗試在msg
找到任何表情符號來替換它們,而是先搜索可能是表情符號的子字符串,然后嘗試僅替換那些表情符號 。
就是說,在表情符號之前或之后使用標點符號的情況確實會失敗; 例如, "I had a beautiful day :)."
因此,總而言之。。 "__EMOT_FROWN"
有很多答案可以為您提供所需的確切信息,但有時我認為您所要求的並不是最佳解決方案。 就像tobias_k所說,最干凈的解決方案是將許多鍵映射到相同的值,本質上是“反轉”您的字典:
emoticons = \
[ ('__EMOT_SMILEY', [':-)', ':)', '(:', '(-:', ] ) ,\
('__EMOT_LAUGH', [':-D', ':D', 'X-D', 'XD', 'xD', ] ) ,\
('__EMOT_LOVE', ['<3', ':\*', ] ) ,\
('__EMOT_WINK', [';-)', ';)', ';-D', ';D', '(;', '(-;', ] ) ,\
('__EMOT_FROWN', [':-(', ':(', '(:', '(-:', ] ) ,\
('__EMOT_CRY', [':,(', ':\'(', ':"(', ':(('] ) ,\
]
emote_dict = {emote: name for name, vals in emoticons for emote in vals}
上面的代碼反轉了字典,因此現在可以像這樣使用它:
>>>print(emote_dict[':)'])
_EMOT_SMILY
您可以嘗試使用dict,只要表情符號中只有2個或3個字符,並且該人使用空格,此方法就可以工作。我確定您可以使其更強大,但現在可以使用。
emoticons = {
'__EMOT_SMILEY': {':-)', ':)', '(:', '(-:'},
'__EMOT_LAUGH' : {':-D', ':D', 'X-D', 'XD', 'xD'},
'__EMOT_LOVE' : {'<3', ':\*'},
'__EMOT_WINK' :{';-)', ';)', ';-D', ';D', '(;', '(-;'},
'__EMOT_FROWN' : {':-(', ':(', '(:', '(-:'},
'__EMOT_CRY' : {':,(', ':\'(', ':"(', ':(('}
}
msg = 'I had a beautiful day :,('
img = msg[-3]
if img[0]==' ':
img = msg[-2:]
else:
img = msg[-3:]
for k, v in emoticons.items():
if img in v:
print(msg[:-3], k)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.