【发布时间】:2016-01-27 12:38:55
【问题描述】:
在perl 中,我可以用空格填充我的标点符号:
s/([،;؛¿!"\])}»›”؟%٪°±©®।॥…])/ $1 /g;`
在Python,我试过这个:
>>> p = u'،;؛¿!"\])}»›”؟%٪°±©®।॥…'
>>> text = u"this, is a sentence with weird» symbols… appearing everywhere¿"
>>> for i in p:
... text = text.replace(i, ' '+i+' ')
...
>>> text
u'this, is a sentence with weird \xbb symbols \u2026 appearing everywhere \xbf '
>>> print text
this, is a sentence with weird » symbols … appearing everywhere ¿
但是有没有办法使用某种占位符符号,例如$1 in perl 我可以在 python 中使用 1 个正则表达式做同样的事情吗?
【问题讨论】:
-
如果您可以发布单独的问题而不是将您的问题合并为一个问题,则最好。这样,它可以帮助人们回答您的问题,也可以帮助其他人至少寻找您的一个问题。谢谢!
-
@kayess,在这种情况下,几乎是同一个问题;我认为问另一个问题会导致重复。尤其是当
\p{Open_Punctuation}有点类似于u""""'<(["""
标签: python regex perl tokenize substitution