问题:如何用下划线替换空格,反之亦然?

我想用字符串中的下划线替换空格以创建漂亮的URL。因此,例如:

"This should be connected" becomes "This_should_be_connected" 

我在Django中使用Python。可以使用正则表达式解决吗?

I want to replace whitespace with underscore in a string to create nice URLs. So that for example:

"This should be connected" becomes "This_should_be_connected" 

I am using Python with Django. Can this be solved using regular expressions?


回答 0

您不需要正则表达式。Python有一个内置的字符串方法可以满足您的需要:

mystring.replace(" ", "_")

You don’t need regular expressions. Python has a built-in string method that does what you need:

mystring.replace(" ", "_")

回答 1

替换空格是可以的,但我建议您进一步处理其他对URL不利的字符,例如问号,撇号,感叹号等。

还要注意,SEO专家之间的普遍共识是,在URL中破折号优先于下划线。

import re

def urlify(s):

    # Remove all non-word characters (everything except numbers and letters)
    s = re.sub(r"[^\w\s]", '', s)

    # Replace all runs of whitespace with a single dash
    s = re.sub(r"\s+", '-', s)

    return s

# Prints: I-cant-get-no-satisfaction"
print(urlify("I can't get no satisfaction!"))

Replacing spaces is fine, but I might suggest going a little further to handle other URL-hostile characters like question marks, apostrophes, exclamation points, etc.

Also note that the general consensus among SEO experts is that dashes are preferred to underscores in URLs.

import re

def urlify(s):

    # Remove all non-word characters (everything except numbers and letters)
    s = re.sub(r"[^\w\s]", '', s)

    # Replace all runs of whitespace with a single dash
    s = re.sub(r"\s+", '-', s)

    return s

# Prints: I-cant-get-no-satisfaction"
print(urlify("I can't get no satisfaction!"))

回答 2

Django具有执行此功能的“ slugify”功能以及其他对URL友好的优化。它隐藏在defaultfilters模块中。

>>> from django.template.defaultfilters import slugify
>>> slugify("This should be connected")

this-should-be-connected

这不完全是您要求的输出,但是IMO最好在URL中使用。

Django has a ‘slugify’ function which does this, as well as other URL-friendly optimisations. It’s hidden away in the defaultfilters module.

>>> from django.template.defaultfilters import slugify
>>> slugify("This should be connected")

this-should-be-connected

This isn’t exactly the output you asked for, but IMO it’s better for use in URLs.


回答 3

这考虑了空格以外的空白字符,我认为它比使用re模块要快:

url = "_".join( title.split() )

This takes into account blank characters other than space and I think it’s faster than using re module:

url = "_".join( title.split() )

回答 4

使用re模块:

import re
re.sub('\s+', '_', "This should be connected") # This_should_be_connected
re.sub('\s+', '_', 'And     so\tshould this')  # And_so_should_this

除非您有多个空格或上述其他空格可能性,否则您可能只想string.replace按照其他人的建议使用即可。

Using the re module:

import re
re.sub('\s+', '_', "This should be connected") # This_should_be_connected
re.sub('\s+', '_', 'And     so\tshould this')  # And_so_should_this

Unless you have multiple spaces or other whitespace possibilities as above, you may just wish to use string.replace as others have suggested.


回答 5

使用字符串的replace方法:

"this should be connected".replace(" ", "_")

"this_should_be_disconnected".replace("_", " ")

use string’s replace method:

"this should be connected".replace(" ", "_")

"this_should_be_disconnected".replace("_", " ")


回答 6

令人惊讶的是,这个图书馆还没有提到

名为python-slugify的python包,可以很好地完成slugizing:

pip install python-slugify

像这样工作:

from slugify import slugify

txt = "This is a test ---"
r = slugify(txt)
self.assertEquals(r, "this-is-a-test")

txt = "This -- is a ## test ---"
r = slugify(txt)
self.assertEquals(r, "this-is-a-test")

txt = 'C\'est déjà l\'été.'
r = slugify(txt)
self.assertEquals(r, "cest-deja-lete")

txt = 'Nín hǎo. Wǒ shì zhōng guó rén'
r = slugify(txt)
self.assertEquals(r, "nin-hao-wo-shi-zhong-guo-ren")

txt = 'Компьютер'
r = slugify(txt)
self.assertEquals(r, "kompiuter")

txt = 'jaja---lol-méméméoo--a'
r = slugify(txt)
self.assertEquals(r, "jaja-lol-mememeoo-a") 

Surprisingly this library not mentioned yet

python package named python-slugify, which does a pretty good job of slugifying:

pip install python-slugify

Works like this:

from slugify import slugify

txt = "This is a test ---"
r = slugify(txt)
self.assertEquals(r, "this-is-a-test")

txt = "This -- is a ## test ---"
r = slugify(txt)
self.assertEquals(r, "this-is-a-test")

txt = 'C\'est déjà l\'été.'
r = slugify(txt)
self.assertEquals(r, "cest-deja-lete")

txt = 'Nín hǎo. Wǒ shì zhōng guó rén'
r = slugify(txt)
self.assertEquals(r, "nin-hao-wo-shi-zhong-guo-ren")

txt = 'Компьютер'
r = slugify(txt)
self.assertEquals(r, "kompiuter")

txt = 'jaja---lol-méméméoo--a'
r = slugify(txt)
self.assertEquals(r, "jaja-lol-mememeoo-a") 

回答 7

我将以下代码用于我的友好网址:

from unicodedata import normalize
from re import sub

def slugify(title):
    name = normalize('NFKD', title).encode('ascii', 'ignore').replace(' ', '-').lower()
    #remove `other` characters
    name = sub('[^a-zA-Z0-9_-]', '', name)
    #nomalize dashes
    name = sub('-+', '-', name)

    return name

Unicode字符也可以正常工作。

I’m using the following piece of code for my friendly urls:

from unicodedata import normalize
from re import sub

def slugify(title):
    name = normalize('NFKD', title).encode('ascii', 'ignore').replace(' ', '-').lower()
    #remove `other` characters
    name = sub('[^a-zA-Z0-9_-]', '', name)
    #nomalize dashes
    name = sub('-+', '-', name)

    return name

It works fine with unicode characters as well.


回答 8

Python在名为replace的字符串上有一个内置方法,其使用方式如下:

string.replace(old, new)

因此,您将使用:

string.replace(" ", "_")

前一段时间我遇到了这个问题,我编写了代码来替换字符串中的字符。我必须开始记得检查python文档,因为它们已经内置了所有功能。

Python has a built in method on strings called replace which is used as so:

string.replace(old, new)

So you would use:

string.replace(" ", "_")

I had this problem a while ago and I wrote code to replace characters in a string. I have to start remembering to check the python documentation because they’ve got built in functions for everything.


回答 9

OP使用的是python,但使用的是javascript(由于语法相似,因此请务必谨慎。

// only replaces the first instance of ' ' with '_'
"one two three".replace(' ', '_'); 
=> "one_two three"

// replaces all instances of ' ' with '_'
"one two three".replace(/\s/g, '_');
=> "one_two_three"

OP is using python, but in javascript (something to be careful of since the syntaxes are similar.

// only replaces the first instance of ' ' with '_'
"one two three".replace(' ', '_'); 
=> "one_two three"

// replaces all instances of ' ' with '_'
"one two three".replace(/\s/g, '_');
=> "one_two_three"

回答 10

mystring.replace (" ", "_")

如果将此值分配给任何变量,它将起作用

s = mystring.replace (" ", "_")

默认情况下,mystring不会有这个

mystring.replace (" ", "_")

if you assign this value to any variable, it will work

s = mystring.replace (" ", "_")

by default mystring wont have this


回答 11

您可以尝试以下方法:

mystring.replace(r' ','-')

You can try this instead:

mystring.replace(r' ','-')

回答 12

perl -e 'map { $on=$_; s/ /_/; rename($on, $_) or warn $!; } <*>;'

匹配并替换空间>​​当前目录中所有文件的下划线

perl -e 'map { $on=$_; s/ /_/; rename($on, $_) or warn $!; } <*>;'

Match et replace space > underscore of all files in current directory


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。