Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all occurrences of several chars from a string

Is there a pythonic way to do what the str.strip() method does, except for all occurrences, not just those at the beginning and end of a string?

Example:

>> '::2012-05-14 18:10:20.856000::'.strip(' -.:')
>> '2012-05-14 18:10:20.856000'

I want

>> '::2012-05-14 18:10:20.856000::'.crazy_function(' -.:')
>> '20120514181020856000'

Does Python provides me a built-in crazy_function???

I could easily do it programatically, but I want to know if there is a built-in for that. Couldn't find one. Thank you for your help.

like image 957
Francisco Avatar asked May 14 '12 21:05

Francisco


People also ask

How do I remove multiple characters from a string?

To remove multiple characters from a string we can easily use the function str. replace and pass a parameter multiple characters. The String class (Str) provides a method to replace(old_str, new_str) to replace the sub-strings in a string. It replaces all the elements of the old sub-string with the new sub-string.

How do you remove all occurrences of a substring from a string?

Example 1: Input: s = "daabcbaabcbc", part = "abc" Output: "dab" Explanation: The following operations are done: - s = "daabcbaabcbc", remove "abc" starting at index 2, so s = "dabaabcbc". - s = "dabaabcbc", remove "abc" starting at index 4, so s = "dababc".


1 Answers

Use the translate function to delete the unwanted characters:

>>> '::2012-05-14 18:10:20.856000::'.translate(None, ' -.:')
'20120514181020856000'

Be sure your string is of str type and not unicode, as the parameters of the function won't be the same. For unicode, use the following syntax ; it consists in building the dict of unicode ordinals from the chars to delete and to map them to None:

>>> u'::2012-05-14 18:10:20.856000::'.translate({ord(k):None for k in u' -.:'})
u'20120514181020856000'

Some timings for performance comparison with re:

>>> timeit.timeit("""re.sub(r"[ -.:]", r"", "'::2012-05-14 18:10:20.856000::'")""","import re")
7.352270301875713
>>> timeit.timeit("""'::2012-05-14 18:10:20.856000::'.translate(None, ' -.:')""")
0.5894893344550951
like image 137
Zeugma Avatar answered Nov 02 '22 05:11

Zeugma