Padding multiple character with space - python

三世轮回 提交于 2019-12-31 04:22:48

问题


In perl, I can do the following with will pad my punctuation symbols with spaces:

s/([،;؛¿!"\])}»›”؟%٪°±©®।॥…])/ $1 /g;` 

In Python, I've tried this:

>>> 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 ¿ 

But is there a way to use some sort of a placeholder symbol, e.g. $1 in perl where I can do the same in python with 1 regex?


回答1:


Python version of $1 is \1, but you should use regex substitution instead of simple string replace:

import re

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

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

Outputs:

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



回答2:


You can use re.sub, with \1 as a placeholder.

>>> 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 ¿



回答3:


Use the format function, and insert a unicode string:

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)

Output

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


来源:https://stackoverflow.com/questions/35037626/padding-multiple-character-with-space-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!