我需要从unicode字符串中删除标点符号。我读过一些帖子,最推荐的是这篇。
我已经实现了以下内容:
table = dict.fromkeys(i for i in range(sys.maxunicode) if unicodedata.category(chr(i)).startswith('P'))
def tokenize(message):
message = unicode(message,'utf-8').lower()
#print message
message = remove_punctuation_unicode(message)
return message
def remove_punctuation_unicode(string):
return string.translate(table)
但当我运行代码时,会弹出以下错误:
table = dict.fromkeys(i for i in range(sys.maxunicode) if unicodedata.category(chr(i)).startswith('P'))
TypeError: must be unicode, not str
我不知道该怎么办。有人能告诉我怎么解决这个问题吗?
尝试unichr
而不是chr
:
Python 2.7.10 (default, Oct 14 2015, 16:09:02)
[GCC 5.2.1 20151010] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys, unicodedata
>>> table = dict.fromkeys(i for i in range(sys.maxunicode) if unicodedata.category(unichr(i)).startswith('P'))
>>>