用空格填充多个字符 - python

时间:2016-01-27 12:38:56

标签: python regex perl tokenize substitution

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 perl python我可以在$scope.clearHistory = function() { $ionicHistory.nextViewOptions({ disableBack: true, historyRoot: true }); } 中使用1个正则表达式执行相同操作吗?

3 个答案:

答案 0 :(得分:2)

$1的Python版本是\1,但您应该使用正则表达式替换而不是简单的字符串替换:

import re

p = ur'([،;؛¿!"\])}»›”؟%٪°±©®।॥…])'
text = u"this, is a sentence with weird» symbols… appearing everywhere¿"

print re.sub(p, ur' \1 ', text)

输出:

this , is a sentence with weird »  symbols …  appearing everywhere ¿ 

答案 1 :(得分:2)

您可以使用re.sub\1作为占位符。

>>> p = u'،;؛¿!"\])}»›”؟%٪°±©®।॥…'
>>> text = u"this, is a sentence with weird» symbols… appearing everywhere¿"
>>> text = re.sub(u'([{}])'.format(p), r' \1 ', text)
>>> print text
this, is a sentence with weird »  symbols …  appearing everywhere ¿

答案 2 :(得分:0)

使用format功能,并插入unicode字符串:

p = u'،;؛¿!"\])}»›”؟%٪°±©®।॥…'
text = u"this, is a sentence with weird» symbols… appearing everywhere¿"
for i in p:
    text = text.replace(i, u' {} '.format(i))

print(text)

<强>输出

this, is a sentence with weird »  symbols …  appearing everywhere ¿