从文本文档中删除标点字符和实体

2024-05-17 12:13:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要删除文本文档中的以下标点字符和实体。你知道吗

  1. 删除&#151&#148&#some number
  2. ; , . ( ) [ ] * ! !
  3. &nbsp

我知道我可以用它来删除&#some number&nbsp。但是,作为一个初学者,我不知道我是否可以做同样的事情来删除其他的东西,比如;,等等

match = re.sub(r'&#146', '', open('test2.txt', 'r').read())

还有,有没有什么方法可以让我一次删除所有代码,而不是多次运行同一个代码。你知道吗


Tags: 代码实体retxtnumbermatchsome文本文档
2条回答

如果字符串中已经有了所有内容,只需使用^{}

>>> s
"hello there ! this is a string with $ some % characters I & don't ( want!"
>>> s.translate(None,"$!%&(")
"hello there  this is a string with  some  characters I  don't  want"

这些看起来像HTML和URL编码的实体。你知道吗

你可以用各种方法解码它们

相关问题 更多 >