Pythonic Way Of Replacing Multiple Characters
Solution 1:
Option 1str.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 2re.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.
Solution 2:
The improvements are pretty straightforward:
Drop lambdas.str.replace() method is a function, and in the first line of your snippet you define a function that calls to another function and nothing else. Why do you need the wrapping lambda? The same concerns the second line.
Use return values. Actually, in docs we see:
Return a copy of the string with all occurrences of substring old replaced by new.
So you can do a first replace(), then do a second one on the obtained result.
To sum up, you'll have:
c = x.replace('\n', '').replace('\t', '').strip()
Note: if you have many characters to remove, you'd better use str.translate() but for two of them str.replace() is far more readable.
Cheers!

Post a Comment for "Pythonic Way Of Replacing Multiple Characters"