Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!

Python3 filename normalize

By: MrPoxipol on May 3rd, 2014  |  syntax: Python  |  size: 0.53 KB  |  views: 149  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import re
  2. from unicodedata import normalize
  3.  
  4. _punct_re = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.:]+')
  5.  
  6. def slugify(text, delim='-'):
  7.     """Generates an slightly worse ASCII-only slug."""
  8.     result = []
  9.     for word in _punct_re.split(text.lower()):
  10.         word = normalize('NFKD', word).encode('ascii', 'ignore')
  11.         word = word.decode('utf-8')
  12.         if word:
  13.             result.append(word)
  14.     return delim.join(result)
  15.    
  16. '''
  17. > print (slugify('zażółć gęślą jaźń'))
  18. > 'zazolc-gesla-jazn'
  19. '''
clone this paste RAW Paste Data