![](/img/trans.png)
[英]python replace all special characters and spaces with single '-'
[英]Remove special characters and replace spaces with “-”
我正在尝试创建一个可以出现在 URL 中的目录。 我想确保它不包含任何特殊字符并用连字符替换任何空格。
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)
它正在删除特殊字符,但是我想使用.replace(" ", "-")
用连字符替换空格
最好的方法可能是使用slugify
function 它将任何字符串作为输入并返回与 URL 兼容的字符串,你的不是 URL 但它会成功,例如:
>>> from django.utils.text import slugify
>>> slugify(' Joel is a slug ')
'joel-is-a-slug'
为什么不使用报价function?
import urllib.parse
urlllib.parse.quote(filename.replace(" ", "-"), safe="")
您可以创建此函数并调用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
它将删除每个非 ASCII 字符和string.punctuation中的每个元素
编辑:这个 function 将用'-'替换string.punctuation中的每个元素,如果你想你可以在 else 语句中用''替换''来合并标点元素之前和之后的两个部分字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.