[Python-ideas] More user-friendly version for string.translate()

Stephen J. Turnbull turnbull.stephen.fw at u.tsukuba.ac.jp
Wed Oct 26 14:24:52 EDT 2016


Mikhail V writes:

 > I need translate() which drops non-defined chars. Please :)

import collections
def translate_or_drop(string, table):
    """
    string: a string to process
    table: a dict as accepted by str.translate
    """
    return string.translate(collections.defaultdict(lambda: None, **table))

All OK now?



More information about the Python-ideas mailing list