替换或删除python中的特定unicode字符

set*_*127 2 python unicode python-2.7

似乎有很多关于在其他语言中这样做的帖子,但我似乎无法弄清楚如何在Python(我使用2.7).

为了清楚起见,我希望将字符串保持为unicode,只需要替换某些特定字符即可.

例如:

thisToken = u'tandh\u2013bm'
print(thisToken)
Run Code Online (Sandbox Code Playgroud)

在中间打印带有m-dash的单词.我只想删除m-dash.(但使用索引,因为我希望能够在找到这些特定字符的任何地方执行此操作.)

我尝试使用replace你喜欢的任何其他角色:

newToke = thisToken.replace('\u2013','')
print(newToke)
Run Code Online (Sandbox Code Playgroud)

但它只是不起作用.任何帮助深表感谢.赛斯

Kev*_*vin 6

您要搜索的字符串也必须是Unicode字符串.尝试:

newToke = thisToken.replace(u'\u2013','')
Run Code Online (Sandbox Code Playgroud)