繁体   English   中英

尝试用 re.sub 替换标点符号但出现错误

[英]Trying to replace punctuation with re.sub but getting error

我在另一个项目上使用 re.sub 取得了成功,但在这个项目上却没有,我不知道为什么。 它不断出现此错误,我不确定它的确切含义。

这是我遇到问题的代码:

import re
def a(sentence):
  sentence=sentence.lower()
  sentence=sentence.split()
  sentence = re.sub(r'[!@#$%&*?]','',sentence)
  print(sentence)
  return

---我得到的错误:

def sub(pattern, repl, string, count=0, flags=0):
"""Return the string obtained by replacing the leftmost
non-overlapping occurrences of the pattern in string by the
replacement repl.  repl can be either a string or a callable;
if a string, backslash escapes in it are processed.  If it is
a callable, it's passed the Match object and must return
a replacement string to be used."""
return _compile(pattern, flags).sub(repl, string, count)

sentence.split()创建一个列表,而re.sub()需要一个字符串。 为什么要拆分?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM