Skip to content

Instantly share code, notes, and snippets.

@C-ArenA
Created September 25, 2021 01:24
Show Gist options
  • Save C-ArenA/efdea6328143cb6797bc46c29ee5f5b2 to your computer and use it in GitHub Desktop.
Save C-ArenA/efdea6328143cb6797bc46c29ee5f5b2 to your computer and use it in GitHub Desktop.
Converts a string into a slug (It works for Spanish language)
def name_to_slug(name:str) -> str:
import unicodedata
lower_cased_name = name.lower()
# replaces spacews by "-"
hyphened_name = '-'.join(lower_cased_name.split())
# eliminates apostrophe
no_apost_name = ''.join(hyphened_name.split("'"))
# eliminates accentuation marks and ñ
# This part info: https://note.nkmk.me/en/python-split-rsplit-splitlines-re/
# https://stackoverflow.com/questions/517923/what-is-the-best-way-to-remove-accents-normalize-in-a-python-unicode-string
nfd_name = unicodedata.normalize('NFD', no_apost_name)
text_name = nfd_name.encode('ascii', 'ignore').decode("utf-8")
return str(text_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment