相关疑难解决方法(0)

Python从除撇号之外的unicode字符串中删除标点符号

我发现了几个这方面的主题,我找到了这个解决方案:

sentence=re.sub(ur"[^\P{P}'|-]+",'',sentence)
Run Code Online (Sandbox Code Playgroud)

这应该删除除了'之外的每个标点符号,问题是它还会删除句子中的所有其他标点符号.

例:

>>> sentence="warhol's art used many types of media, including hand drawing, painting, printmaking, photography, silk screening, sculpture, film, and music."
>>> sentence=re.sub(ur"[^\P{P}']+",'',sentence)
>>> print sentence
'
Run Code Online (Sandbox Code Playgroud)

当然我想要的是保持句子没有标点符号,"warhol"保持原样

期望的输出:

"warhol's art used many types of media including hand drawing painting printmaking photography silk screening sculpture film and music"
"austro-hungarian empire"
Run Code Online (Sandbox Code Playgroud)

编辑:我也试过用

tbl = dict.fromkeys(i for i in xrange(sys.maxunicode)
    if unicodedata.category(unichr(i)).startswith('P')) 
sentence = sentence.translate(tbl)
Run Code Online (Sandbox Code Playgroud)

但这会删除每个标点符号

python regex unicode punctuation

9
推荐指数
1
解决办法
6357
查看次数

标签 统计

punctuation ×1

python ×1

regex ×1

unicode ×1