如果在python中包含\ u,则删除字符串中的单词? [关闭]

问题描述 投票:-4回答:1

如果要在python中包含\ u,则要删除字符串中的单词?

ex:

string ="\uf064thickness cfoutside\uf0d7\uf03a\uf03d TC2019 45TRCMat"

最终输出应该是这样。

"TC2019 45TRCMat"

删除所有单词后,如果包含\ u。

python regex python-unicode
1个回答
1
投票

而不是试图删除unicode字符,而是只允许使用ascii字符:

string ="\uf064thickness cfoutside\uf0d7\uf03a\uf03d TC2019 45TRCMat"

def is_ascii(s):
    return all(ord(c) < 128 for c in s)

for s in string.split(" "):
    if is_ascii(s):
        print(s)

参考:How to check if a string in Python is in ASCII?

© www.soinside.com 2019 - 2024. All rights reserved.