Created
September 7, 2012 10:51
-
-
Save scraperdragon/3665070 to your computer and use it in GitHub Desktop.
Make an identifier out of a string
This file contains 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
def makeidentifier(s): | |
import string | |
s=s.strip().replace(' ','_') | |
valid_chars = "_%s%s" % (string.ascii_letters, string.digits) | |
out=''.join(c for c in s if c in valid_chars) | |
if len(out)==0: | |
return '_' | |
else: | |
return out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment