Skip to content

Instantly share code, notes, and snippets.

@knu2xs
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knu2xs/8786365 to your computer and use it in GitHub Desktop.
Save knu2xs/8786365 to your computer and use it in GitHub Desktop.
Replaces or drops invalid characters from names according to rules for naming feature classes
def fix_invalid_characters(self, string):
"""
Drops invalid characters from names according to rules for naming feature classes, notably must
start with a letter and remaining characters must be numbers, letters or underscores.
"""
# drop invalid characters at beginning of string
string = re.sub(r'^[^a-zA-Z]+', '', string)
# find any invalid characters in remainder of string and replace with underscores
return re.sub(r'[^a-z0-9A-Z_]', '_', string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment