Python - 仅保留字母数字和空格,并忽略非 ASCII [英] Python - keep only alphanumeric and space, and ignore non-ASCII
本文介绍了Python - 仅保留字母数字和空格,并忽略非 ASCII的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用这一行来删除除空格之外的所有非字母数字字符
I have this line to remove all non-alphanumeric characters except spaces
re.sub(r'\W+', '', s)
尽管如此,它仍然保留了非英文字符.
Although, it still keeps non-English characters.
例如如果我有
re.sub(r'\W+', '', 'This is a sentence, and here are non-english 托利 苏 !!11')
我想获得作为输出:
> 'This is a sentence and here are non-english 11'
推荐答案
re.sub(r'[^A-Za-z0-9 ]+', '', s)
(编辑)澄清:[]
创建一个字符列表.^
否定列表.A-Za-z
是英文字母, 是空格.对于其中的任何一个或多个(即不是 A-Z、a-z 或空格的任何内容),请替换为空字符串.
(Edit) To clarify:
The []
create a list of chars. The ^
negates the list. A-Za-z
are the English alphabet and is space. For any one or more of these (that is, anything that is not A-Z, a-z, or space,) replace with the empty string.
这篇关于Python - 仅保留字母数字和空格,并忽略非 ASCII的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文