我有一个函数可以从字符串列表中删除标点符号:
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)
将其转换为字符串列表。 - jfs