SHARE
TWEET

Python3 filename normalize

MrPoxipol May 3rd, 2014 236 Never
  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. '''
RAW Paste Data
Top