我正在尝试使用字典翻译多个字符串;但是,它总是替换每个单独的字符,而且我不知道如何调整我的代码。
我的字典:
{"You're": "I'm", "We've": "you've", 'am': 'are', "We'll": "you'll", 'im': "you're",
"we'd": "you'd", 'our': 'your', 'You': 'I', 'Was': 'were', 'your': 'my', "you're":
"I'm", 'We': 'you', "I've": "you've", "we've": "you've", 'This': 'that', "we're":
"you're", 'you': 'I', 'was': 'were', 'me': 'you', 'we': 'you', 'I': 'you', 'c': 'see',
"I'd": "you'd", 'Were': 'was', "I'm": "you're", 'My': 'your', "I'll": "you'll", "we'll":
"you'll", 'this': 'that', 'Am': 'are', 'ur': "I'm", 'i': 'you', 'u': 'me', "We'd":
"you'd", 'were': 'was', 'Our': 'your', "i'm": "you're", 'my': 'your', 'Your': 'my',
"We're": "you're"}
我的代码:
def replace_all(text, dic):
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
稍后由以下人员调用:
message = replace_all(message, dictionary)
是否可以替换字符串中的整个单词?我对python很陌生,所以任何帮助将不胜感激!