简体   繁体   中英

Remove special characters and replace spaces with “-”

I'm trying to make a directory that is okay to appear in a 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

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:

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

Why don't you use the quote function?

import urllib.parse

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

You can create this functions and call remove_special_chars(s) to do it:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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