我想问一些关于使用python翻译somestring的问题。我有一个csv文件,包含类似这样的删节词典列表
before, after
ROFL, Rolling on floor laughing
STFU, Shut the freak up
LMK, Let me know
...
我想将“before”列中包含单词的字符串转换为“after”列中的单词。我尝试使用这段代码,但它没有改变任何东西
def replace_abbreviation(tweet):
dictionary = pd.read_csv("dict.csv", encoding='latin1')
dictionary['before'] = dictionary['before'].apply(lambda val: unicodedata.normalize('NFKD', val).encode('ascii', 'ignore').decode())
tmp = dictionary.set_index('before').to_dict('split')
tweet = tweet.translate(tmp)
return tweet
例如:
您可以将内容读入dict,然后使用以下代码
输出
相关问题 更多 >
编程相关推荐