【发布时间】:2017-08-21 05:55:59
【问题描述】:
我有一个从字符串列表中删除标点符号的函数:
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 格式的字符串中删除标点符号?
【问题讨论】:
-
strip_punctuation()应该接受字符串而不是字符串列表,然后如果您需要它可以list_of_strings = map(strip_punctuation, list_of_strings) -
这实际上可能是一个更好的方法。我喜欢你和 F.C. 使用 unicode 类别的实现。