1

我正在使用Python 3 中字符串模块的maketrans来进行简单的文本预处理,例如降低、删除数字和标点符号。问题是在删除标点符号的过程中,所有单词都连接在一起,没有空格!例如,假设我有以下文本:

text='[{"Hello":"List:","Test"321:[{"Hello":"Airplane Towel for Kitchen"},{"Hello":2 " Repair massive utilities "2},{"Hello":"Some 3 appliance for our kitchen"2}'

text=text.lower() text=text.translate(str.maketrans(' ',' ',string.digits))

工作得很好,它给出了:

'[{"hello":"list:","test":[{"hello":"airplane towel for kitchen"},{"hello": " repair massives utilities "},{"hello":"some  appliance for our kitchen"}'

但是一旦我想删除标点符号:

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)
4

1 回答 1

3

嗯......这行得通

txt = text.translate(str.maketrans(string.punctuation, ' ' * len(string.punctuation))).replace(' '*4, ' ').replace(' '*3, ' ').replace(' '*2, ' ').strip()
于 2018-10-05T15:15:52.420 回答