String slugification in Python

前端 未结 10 1083
粉色の甜心
粉色の甜心 2020-11-30 23:10

I am in search of the best way to \"slugify\" string what \"slug\" is, and my current solution is based on this recipe

I have changed it a little bit to:

<         


        
10条回答
  •  死守一世寂寞
    2020-12-01 00:04

    Install unidecode form from here for unicode support

    pip install unidecode

    # -*- coding: utf-8 -*-
    import re
    import unidecode
    
    def slugify(text):
        text = unidecode.unidecode(text).lower()
        return re.sub(r'[\W_]+', '-', text)
    
    text = u"My custom хелло ворлд"
    print slugify(text)
    

    >>> my-custom-khello-vorld

提交回复
热议问题