替换python中列表中的特殊字符

时间:2017-08-22 13:50:55

标签: python replace

如何使用给定功能替换特殊字符(表情符号)。

例如

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

我知道如何使用dict,但在这里我有多个与每个功能相关联的值

以下代码在这种情况下不起作用

for emote, replacement in emoticons.items():
  msg = msg.replace(emote, replacement)

7 个答案:

答案 0 :(得分:2)

您可以使用词典 regex

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

答案 1 :(得分:1)

这可以做到:

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])

所以,你创建一个字典,翻转它并替换句子中存在的所有表情符号。

答案 2 :(得分:0)

请改为尝试:

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

答案 3 :(得分:0)

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)

答案 4 :(得分:0)

这个怎么样:

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"

答案 5 :(得分:0)

有很多答案可以满足您的要求,但有时候我认为您所要求的并不是最佳解决方案。就像tobias_k所说的那样,最干净的解决方案是将许多键映射到相同的值,基本上是#34;反转&#34;你的字典:

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

答案 6 :(得分:0)

你可以尝试使用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)