Skip to content

Instantly share code, notes, and snippets.

@cizixs
Last active December 20, 2015 16:49
Show Gist options
  • Select an option

  • Save cizixs/6164008 to your computer and use it in GitHub Desktop.

Select an option

Save cizixs/6164008 to your computer and use it in GitHub Desktop.
Python translate method wrapper, from Python Cookbook
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