当前位置:   article > 正文

python的文件名规范,在Python中将Unicode文本规范化为文件名等

word = unicodedata.normalize('nfkd', word)

Are there any standalonenish solutions for normalizing international unicode text to safe ids and filenames in Python?

E.g. turn My International Text: åäö to my-international-text-aao

plone.i18n does really good job, but unfortunately it depends on zope.security and zope.publisher and some other packages making it fragile dependency.

解决方案

What you want to do is also known as "slugify" a string. Here's a possible solution:

import re

from unicodedata import normalize

_punct_re = re.compile(r'[\t !"#$%&\'()*\-/<=>?@

^_`{|},.:]+')

def slugify(text, delim=u'-'):

"""Generates an slightly worse ASCII-only slug."""

result = []

for word in _punct_re.split(text.lower()):

word = normalize('NFKD', word).encode('ascii', 'ignore')

if word:

result.append(word)

return unicode(delim.join(result))

Usage:

>>> slugify(u'My International Text: åäö')

u'my-international-text-aao'

You can also change the delimeter:

>>> slugify(u'My International Text: åäö', delim='_')

u'my_international_text_aao'

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/204034
推荐阅读
相关标签
  

闽ICP备14008679号