简体   繁体   English

删除特殊字符并用“-”替换空格

[英]Remove special characters and replace spaces with “-”

I'm trying to make a directory that is okay to appear in a URL.我正在尝试创建一个可以出现在 URL 中的目录。 I want to ensure that it doesn't contain any special characters and replace any spaces with hyphens.我想确保它不包含任何特殊字符并用连字符替换任何空格。

from os.path import join as osjoin
def image_dir(self, filename):
    categorydir = ''.join(e for e in  str(self.title.lower()) if e.isalnum())
    return "category/" + osjoin(categorydir, filename)

It's removing special characters however I'd like use .replace(" ", "-") to swap out spaces with hyphens它正在删除特殊字符,但是我想使用.replace(" ", "-")用连字符替换空格

The best way is probably to use the slugify function which takes any string as input and returns an URL-compatible one, yours is not an URL but it will do the trick, eg:最好的方法可能是使用slugify function 它将任何字符串作为输入并返回与 URL 兼容的字符串,你的不是 URL 但它会成功,例如:

>>> from django.utils.text import slugify
>>> slugify(' Joel is a slug ')
'joel-is-a-slug'

Why don't you use the quote function?为什么不使用报价function?

import urllib.parse

urlllib.parse.quote(filename.replace(" ", "-"), safe="")

You can create this functions and call remove_special_chars(s) to do it:您可以创建此函数并调用remove_special_chars(s)来执行此操作:

def __is_ascii__(c):
    return (ord(c) < 128)


def remove_special_chars(s):
    output = ''

    for c in s:
        if (c.isalpha() and __is_ascii__(c)) or c == ' ':
            output = output + c
        else:
            if c in string.punctuation:
                output = output + ' '

    output = re.sub(' +', ' ', output)
    output = output.replace(' ', '-')

    return output

It will remove each non-ASCII character and each element in string.punctuation它将删除每个非 ASCII 字符和string.punctuation中的每个元素

EDIT: This function will substitute each element in string.punctuation with a '-', if you want you can substitute ' ' with '' in the else statement to merge the two parts of the string before and after the punctuation element.编辑:这个 function 将用'-'替换string.punctuation中的每个元素,如果你想你可以在 else 语句中用''替换''来合并标点元素之前和之后的两个部分字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM