繁体   English   中英

在python中替换unicode

[英]Replacing unicode in python

这是我用python打印出来的结果:

With \ all\ respect, if we look from one perspective, it is just like looking at ants.

并且数据类型是

<type 'unicode'>

有没有办法''替换\ 我试过了

str.replace("\ ", '') , str.replace("\\\ ", '') str.replace("<b>", '')但它们都str.replace("\\\ ", '')

. 如何用空字符串正确替换它?

编辑:

这是print repr(mystrung)的结果:

With \\u003cb\\u003eall\\u003c/b\\u003e respect, if we look from one
perspective, it is just like looking at ants.

如果你真的想完全删除它们,你的第二个例子应该有效。 但是,使用 Unicode 字符串效率更高,因为消除了隐式转换:

>>> s=u'With \\u003cb\\u003eall\\u003c/b\\u003e respect, if we look from one perspective, it is just like looking at ants.'
>>> s.replace(u'\\u003cb\\u003e',u'').replace(u'\\u003c/b\\u003e',u'')
u'With all respect, if we look from one perspective, it is just like looking at ants.'

如果您只想转换 Unicode 转义符,使用ascii编码仅包含 ASCII 代码点的 Unicode 字符串会将其转换回字节字符串,然后使用unicode-escape对其进行解码以将文字转义码转换回字符:

>>> print(s.encode('ascii').decode('unicode-escape'))
With <b>all</b> respect, if we look from one perspective, it is just like looking at ants.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM