Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从PYTHON中的字符串创建SEO友好的破折号分隔url?_Python_Url - Fatal编程技术网

如何从PYTHON中的字符串创建SEO友好的破折号分隔url?

如何从PYTHON中的字符串创建SEO友好的破折号分隔url?,python,url,Python,Url,基本上,我的问题与Python(和GAE)相同,但不是C# 要求: 用破折号分隔每个单词并删除所有标点符号(考虑到并非所有单词都用空格分隔。) 函数接受最大长度,并获取低于该最大长度的所有令牌。示例:ToSeoFriendly(“hello world hello world”,14)返回“hello world” 所有单词都转换成小写 您正在搜索的术语是“”。作为替代(可能是更经过测试的版本),我建议您使用Django提供的(最小修改)slugify代码: import unicodedat

基本上,我的问题与Python(和GAE)相同,但不是C#

要求:

  • 用破折号分隔每个单词并删除所有标点符号(考虑到并非所有单词都用空格分隔。)
  • 函数接受最大长度,并获取低于该最大长度的所有令牌。示例:ToSeoFriendly(“hello world hello world”,14)返回“hello world”
  • 所有单词都转换成小写

您正在搜索的术语是“”。

作为替代(可能是更经过测试的版本),我建议您使用Django提供的(最小修改)slugify代码:

import unicodedata
import re

def slugify(value):
    """
    Normalizes string, converts to lowercase, removes non-alpha characters,
    and converts spaces to hyphens.
    """
    value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
    value = re.sub('[^\w\s-]', '', value).strip().lower()
    return re.sub('[-\s]+', '-', value)

请参阅:

注意:您没有说搜索引擎优化友好型,而是说搜索引擎友好型,因为搜索引擎优化是搜索引擎优化,这是合乎逻辑的,但现在您刚刚将术语的字母数增加了一倍。那么“搜索引擎友好型”呢?
import unicodedata
import re

def slugify(value):
    """
    Normalizes string, converts to lowercase, removes non-alpha characters,
    and converts spaces to hyphens.
    """
    value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
    value = re.sub('[^\w\s-]', '', value).strip().lower()
    return re.sub('[-\s]+', '-', value)