gpt4 book ai didi

python - 删除文本中除 "\n"和 "/"之外的所有特殊字符

转载 作者:行者123 更新时间:2023-12-01 07:50:05 25 4
gpt4 key购买 nike

从这篇文章中,我找到了如何从文本中删除除空格和字母数字之外的所有内容:Python: Strip everything but spaces and alphanumeric .

这样:

re.sub(r'([^\s\w]|_)+', '', document)

我基本上想删除所有特殊字符。

但是,现在我想做同样的事情(即删除所有特殊字符),但不删除以下特殊字符:

  1. \n
  2. /

我该怎么做?

最佳答案

我们可以尝试在不使用严格字符类的情况下重写您的模式:

document = "Hello!@#$/ World!"
output = re.sub(r'[^ \nA-Za-z0-9/]+', '', document)
print(output)

Hello/ World

这表示删除任何非字母数字、空格、换行符或正斜杠的字符。

关于python - 删除文本中除 "\n"和 "/"之外的所有特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56279343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com