Last active
December 20, 2015 16:49
-
-
Save cizixs/6164008 to your computer and use it in GitHub Desktop.
Python translate method wrapper, from Python Cookbook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import string | |
| def translator(frm='', to='', delete='', keep=None): | |
| if len(to) == 1: | |
| to = to * len(frm) | |
| trans = string.maketrans(frm, to) | |
| if keep is not None: | |
| allchars = string.maketrans('', '') | |
| delete = allchars.translate(allchars, keep.translate(allchars,delete)) | |
| def translate(s): | |
| return s.translate(trans, delete) | |
| return translate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment