【发布时间】:2020-07-20 17:07:28
【问题描述】:
我想将两行代码合二为一。
第一个是删除所有string.punctuations。我使用的代码如下:
df[col].apply(lambda x: re.sub(r'[!\"#$%&\'()*+,-.\/:;<=>?@[\\]^_`{|}~]+', '', x))
第二个是去掉一些特殊字符(我不知道如何表达这种像“’‘”这样的双引号;这些和普通的'""'引号不同):
df[col].apply(lambda x: re.sub(r'[“’‘”]', '', x))
我想在一行代码中将它们全部删除。我试图简单地将第二个完全匹配添加到第一个,但事实证明第二个匹配没有在文本中删除。我想知道为什么以及如何有效地删除那些punctuations。
需要清理的示例文本可能是:
text = '“Client” refers to Client or “”any User uploads or otherwise supplies to, or stores in, the Services under Client’s account.'
【问题讨论】:
-
试试这个,
df.col.str.replace(r"[^\w+|\d+]", "")