如何在此代码中用 space 替换标点符号?

How to replace a punctuation with a space in this code?

我有这个代码:

 def remove_punctuation(self,text):
        exclude = set(string.punctuation)
        a=''.join(ch for ch in text if ch not in exclude)
        return ''.join(c for c in a if not ud.category(c).startswith('P'))

首先我想知道这是做什么的:

ch for ch in text if ch not in exclude

怎么可能写出这样的 for 循环?

其次,我想替换那些标点符号,让我们在这样的文本中说: "hello_there?my_friend!" 和 space 使用上面的代码。我该如何更改该代码才能做到这一点?

代码段:

a = ''.join([ch for ch in text if ch not in exclude])

相当于

string_without_punctuation = ''
exclude = set(string.punctuation) # =set('!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~')
for character in text:
    if character not in exclude:
        string_without_punctuation += character

您可以简单地执行此操作以将标点符号替换为空格:

string_without_punctuation = ''
exclude = set(string.punctuation) # =set('!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~')
for character in text:
    if character not in exclude:
        string_without_punctuation += character
    else:
        string_without_punctuation += ' '

我建议使用 str.translate 而不是手动重建字符串。查找 table 将字符映射到您要替换它们的字符串。

trans = str.maketrans(dict.fromkeys(string.punctuation, ' '))

"hello_there?my_friend!".translate(trans)
# 'hello there my friend '