从 Python 字符串中删除零宽度空格 unicode 字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46154561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove zero width space unicode character from Python string
提问by V.Anh
I have a string in Python like this:
我在 Python 中有一个这样的字符串:
u'\u200cHealth & Fitness'
How can i remove the
我怎样才能删除
\u200c
part from the string ?
从字符串的一部分?
回答by Arount
You can encode it into ascii
and ignore errors:
您可以将其编码为ascii
并忽略错误:
u'\u200cHealth & Fitness'.encode('ascii', 'ignore')
Output:
输出:
'Health & Fitness'
回答by Hayat
If you have a string that contains Unicode
character, like
如果您有一个包含Unicode
字符的字符串,例如
s = "Airports Council International \u2013 North America"
then you can try:
那么你可以尝试:
newString = (s.encode('ascii', 'ignore')).decode("utf-8")
and the output will be:
输出将是:
Airports Council International North America
Airports Council International North America
Upvote if helps:)
如果有帮助,请点赞:)
回答by Diana
for me the following worked
对我来说以下工作
mystring.encode('ascii', 'ignore').decode('unicode_escape')
回答by Sitti Munirah Abdul Razak
I just use replace because I don't need it:
我只是使用替换因为我不需要它:
varstring.replace('\u200c', '')
Or in your case:
或者在你的情况下:
u'\u200cHealth & Fitness'.replace('\u200c', '')