String slugification in Python

前端 未结 10 1074
粉色の甜心
粉色の甜心 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-11-30 23:40

    A couple of options on GitHub:

    1. https://github.com/dimka665/awesome-slugify
    2. https://github.com/un33k/python-slugify
    3. https://github.com/mozilla/unicode-slugify

    Each supports slightly different parameters for its API, so you'll need to look through to figure out what you prefer.

    In particular, pay attention to the different options they provide for dealing with non-ASCII characters. Pydanny wrote a very helpful blog post illustrating some of the unicode handling differences in these slugify'ing libraries: http://www.pydanny.com/awesome-slugify-human-readable-url-slugs-from-any-string.html This blog post is slightly outdated because Mozilla's unicode-slugify is no longer Django-specific.

    Also note that currently awesome-slugify is GPLv3, though there's an open issue where the author says they'd prefer to release as MIT/BSD, just not sure of the legality: https://github.com/dimka665/awesome-slugify/issues/24

    0 讨论(0)
  • 2020-11-30 23:41

    There is python package named awesome-slugify:

    pip install awesome-slugify
    

    Works like this:

    from slugify import slugify
    
    slugify('one kožušček')  # one-kozuscek
    

    awesome-slugify github page

    0 讨论(0)
  • 2020-11-30 23:51

    You might consider changing the last line to

    slug=re.sub(r'--+',r'-',slug)
    

    since the pattern [-]+ is no different than -+, and you don't really care about matching just one hyphen, only two or more.

    But, of course, this is quite minor.

    0 讨论(0)
  • 2020-11-30 23:53

    There is a python package named python-slugify, which does a pretty good job of slugifying:

    pip install python-slugify
    

    Works like this:

    from slugify import slugify
    
    txt = "This is a test ---"
    r = slugify(txt)
    self.assertEquals(r, "this-is-a-test")
    
    txt = "This -- is a ## test ---"
    r = slugify(txt)
    self.assertEquals(r, "this-is-a-test")
    
    txt = 'C\'est déjà l\'été.'
    r = slugify(txt)
    self.assertEquals(r, "cest-deja-lete")
    
    txt = 'Nín hǎo. Wǒ shì zhōng guó rén'
    r = slugify(txt)
    self.assertEquals(r, "nin-hao-wo-shi-zhong-guo-ren")
    
    txt = 'Компьютер'
    r = slugify(txt)
    self.assertEquals(r, "kompiuter")
    
    txt = 'jaja---lol-méméméoo--a'
    r = slugify(txt)
    self.assertEquals(r, "jaja-lol-mememeoo-a")
    

    See More examples

    This package does a bit more than what you posted (take a look at the source, it's just one file). The project is still active (got updated 2 days before I originally answered, over seven years later (last checked 2020-06-30), it still gets updated).

    careful: There is a second package around, named slugify. If you have both of them, you might get a problem, as they have the same name for import. The one just named slugify didn't do all I quick-checked: "Ich heiße" became "ich-heie" (should be "ich-heisse"), so be sure to pick the right one, when using pip or easy_install.

    0 讨论(0)
  • 2020-11-30 23:54

    It works well in Django, so I don't see why it wouldn't be a good general purpose slugify function.

    Are you having any problems with it?

    0 讨论(0)
  • 2020-11-30 23:57

    Another option is boltons.strutils.slugify. Boltons has quite a few other useful functions as well, and is distributed under a BSD license.

    0 讨论(0)
提交回复
热议问题