删除未知的特殊字符

时间:2019-01-18 05:21:25

标签: python special-characters

删除特殊字符

 s="____Ç_apple___   _______new A_____"

 print(re.sub('[^0-9a-zA-Z]\s+$', '', s))

结果= ______________________apple___ _______新A _____

 s="____Ç_apple___   _______new A_____"

 print(re.sub('[^0-9a-zA-Z]', '', s))

result = applenewA

最终 结果=苹果新A

但是我不明白

我要删除Ç和_并保持空格和英文

2 个答案:

答案 0 :(得分:3)

由于要将多个空格合并为一个空格,然后删除不是单词或空格的字符,因此应使用两个单独的正则表达式替换来实现:

apple new A

这将输出:

git config credential.helper

答案 1 :(得分:0)

您要使用'apple new A'作为结果,对吧?

s="____Ç_apple___   _______new A_____"

result = re.sub('[^a-zA-Z|\s]+', '', s)  # apple   new A
result = ' '.join(result.split())  # apple new A
print(result)