我使用python3中的maketrans中的string module执行简单的文本预处理,比如降低、删除数字和标点符号。问题是在删除标点的过程中,所有的单词都被连接在一起,没有空格!例如,假设我有以下文本:
text='[{"Hello":"List:","Test"321:[{"Hello":"Airplane Towel for Kitchen"},{"Hello":2 " Repair massive utilities "2},{"Hello":"Some 3 appliance for our kitchen"2}'
文本=文本.下() 文本=文本.翻译(str.maketrans公司(' ',' ',字符串.数字))在
效果很好,它提供:
^{pr2}$但一旦我想去掉标点符号:
text=text.translate(str.maketrans(' ',' ',string.punctuation))
它给我的是:
'hellolisttesthelloairplane towel for kitchenhello nbsprepair massives utilitiesnbsphellosome appliance for our kitchen'
理想情况下,它应该产生:
'hello list test hello airplane towel for kitchen hello nbsp repair massives utilities nbsp hello some appliance for our kitchen'
我用maketrans并没有具体的原因,但我喜欢它的快速和简单,并有点坚持解决它。谢谢!在
免责声明:我已经知道如何使用re进行操作,如下所示:
import re
s = "string.]With. Punctuation?"
s = re.sub(r'[^\w\s]','',s)
嗯。。。这很管用
相关问题 更多 >
编程相关推荐