String slugification in Python

前端 未结 10 1093
粉色の甜心
粉色の甜心 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:03

    The problem is with the ascii normalization line:

    slug = unicodedata.normalize('NFKD', s)
    

    It is called unicode normalization which does not decompose lots of characters to ascii. For example, it would strip non-ascii characters from the following strings:

    Mørdag -> mrdag
    Æther -> ther
    

    A better way to do it is to use the unidecode module that tries to transliterate strings to ascii. So if you replace the above line with:

    import unidecode
    slug = unidecode.unidecode(s)
    

    You get better results for the above strings and for many Greek and Russian characters too:

    Mørdag -> mordag
    Æther -> aether
    

提交回复
热议问题