def slugify(value, allow_unicode=False):
"""Slugify string to make it a valid filename.
Convert to ASCII if 'allow_unicode' is False. Convert spaces to hyphens.
Remove characters that aren't alphanumerics, underscores, or hyphens.
Also strip leading and trailing whitespace.
"""
import unicodedata
value = str(value)
if allow_unicode:
value = unicodedata.normalize('NFKC', value)
else:
value = unicodedata.normalize('NFKD', value).encode(
'ascii', 'ignore').decode('ascii')
value = re.sub(r'[^\w\s-]', '', value).strip()
return re.sub(r'[-\s]+', '-', value)
# Below from
# http://stackoverflow.com/questions/2333872/atomic-writing-to-file-with-python
评论列表
文章目录