Advertisement
MrPoxipol

Python3 filename normalize

May 3rd, 2014
1,153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.53 KB | None | 0 0
  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. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement