我有一个函数可以从字符串列表中删除标点符号:
def strip_punctuation(input):
x = 0
for word in input:
input[x] = re.sub(r'[^A-Za-z0-9 ]', "", input[x])
x += 1
return input
我最近修改了我的脚本以使用 Unicode 字符串,这样我就可以处理其他非西方字符。此函数在遇到这些特殊字符时中断,并只返回空的 Unicode 字符串。如何可靠地从 Unicode 格式的字符串中删除标点符号?
原文由 acpigeon 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以使用
unicode.translate()
方法:您还可以使用 正则表达式模块 支持的
r'\p{P}'
: