替换商标符号(™) 当阿隆

2024-06-06 20:59:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把商标符号去掉(™) 但只有在它后面没有任何其他符号的情况下,例如我可能有€™ 这是一个错误的引号(')编码,所以我不想删除商标符号(™) 所以我用这个模式来代替xx™ 带引号。你知道吗

dict = {};

chars = {
    '\xe2\x84\xa2': '', # ™
    '\xe2\x80\x99': "'", # ’
        }

def stats_change(char, number):
  if dict.has_key(char):
    dict[char] = dict[char]+number
  else:
    dict[char] = number # Add new entry

def replace_chars(match):
  char = match.group(0)
  stats_change(char,1)

  return chars[char]

i, nmatches = re.subn("(\\" + '|\\'.join(chars.keys()) + ")", replace_chars, i)
count_matches += nmatches

输入:foo™ oof
输出:foo oof

输入:o’f oof
输出:o'f oof

有什么建议吗?你知道吗


Tags: numberdefstatsmatch符号changedictreplace