我想清理我的评论数据。下面是我的代码:
def processData(data):
data = data.lower() #casefold
data = re.sub('<[^>]*>',' ',data) #remove any html
data = re.sub(r'#([^\s]+)', r'\1', data) #Replace #word with word
remove = string.punctuation
remove = remove.replace("'", "") # don't remove '
p = r"[{}]".format(remove) #create the pattern
data = re.sub(p, "", data)
data = re.sub('[\s]+', ' ', data) #remove additional whitespaces
pp = re.compile(r"(.)\1{1,}", re.DOTALL) #pattern for remove repetitions
data = pp.sub(r"\1\1", data)
return data这段代码几乎工作得很好,但仍然存在一个问题。对于“她在公共服务部门工作”这句话,
我拿到了“她在公共服务部门工作”。
问题是字符串标点后没有空格。
我希望我的句子是这样的:“她从事公共服务”。
你能帮我写代码吗?
发布于 2016-12-07 18:51:51
我想你想要的是:
>>> st = 'she works in public-service'
>>> import re
>>> re.sub(r'([{}])'.format(string.punctuation),r' ',st)
'she works in public service'
>>> https://stackoverflow.com/questions/41015075
复制相似问题