在python中创建slug

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在python中创建slug相关的知识,希望对你有一定的参考价值。

A slug function that support acentuation :)
  1. # -*- coding: utf-8 -*-
  2. import htmlentitydefs, re
  3.  
  4. def slugfy(text, separator):
  5. ret = ""
  6. for c in text.lower():
  7. try:
  8. ret += htmlentitydefs.codepoint2name[ord(c)]
  9. except:
  10. ret += c
  11.  
  12. ret = re.sub("([a-zA-Z])(uml|acute|grave|circ|tilde|cedil)", r"1", ret)
  13. ret = re.sub("W", " ", ret)
  14. ret = re.sub(" +", separator, ret)
  15.  
  16. return ret.strip()
  17.  
  18. # usage @ pt_BR
  19. print slugfy("Teste de acentuação python", "-")

以上是关于在python中创建slug的主要内容,如果未能解决你的问题,请参考以下文章