Pythonic Way of replacing Multiple Characters

后端 未结 2 525
我寻月下人不归
我寻月下人不归 2021-01-15 22:22

I\'ve created a onetime function

a = lambda x: x.replace(\'\\n\', \'\')
b = lambda y: y.replace(\'\\t\', \'\').strip()
c = lambda x: b(a(x))
<
2条回答
  •  萌比男神i
    2021-01-15 22:50

    Option 1
    str.translate
    For starters, if you're replacing a lot of characters with the same thing, I'd 100% recommend str.translate.

    >>> from string import whitespace as wsp
    >>> '\n\ttext   \there\r'.translate(str.maketrans(dict.fromkeys(wsp, '')))
    'texthere'
    

    This syntax is valid with python-3.x only. For python-2.x, you will need to import string and use string.maketrans to build the mapping instead.

    If you want to exclude whitespace chars itself, then

    wsp = set(wsp) - {' '}
    

    Option 2
    re.sub
    The regex equivalent of the above would be using re.sub.

    >>> import re
    >>> re.sub(r'\s+', '', '\n\ttext   \there\r')
    'texthere'
    

    However, performance wise, str.translate beats this hands down.

提交回复
热议问题