Python/Django:如何从字符串中删除额外的空格和制表符?

时间:2022-12-09 07:23:43

I'm building a website with Python/Django. Users submit tags. Each tag can contain multiple words. Each tag has an ID number. I want to make sure tags that are formatted slightly differently are still being recognized as the same tag.

我正在用Python/Django构建一个网站。用户提交标记。每个标签可以包含多个单词。每个标签都有一个ID号。我希望确保格式化略有不同的标记仍然被识别为相同的标记。

For example, if one user submitted the tag "electric guitar" and the other submitted "electric   guitar" (2 white spaces between the 2 words) I want to be able to recognize they are the same tag.

例如,如果一个用户提交了标签“电吉他”,而另一个用户提交了“电吉他”(两个单词之间有两个空格),我希望能够识别它们是相同的标签。

How to I remove all the extra white spaces and tabs in this case? Thanks.

在这种情况下,如何删除所有多余的空格和制表符?谢谢。

6 个解决方案

#1


51  

Split on any whitespace, then join on a single space.

在任何空格上拆分,然后加入一个空格。

' '.join(s.split())

#2


20  

>>> import re
>>> re.sub(r'\s+', ' ', 'some   test with     ugly  whitespace')
'some test with ugly whitespace'

#3


7  

I would use Django's slugify method, which condenses spaces into a single dash and other helpful features:

我将使用Django的slugify方法,将空间压缩为一个单一的dash和其他有用的特性:

from django.template.defaultfilters import slugify

#4


1  

"electric guitar".split() will give you ['electric', 'guitar']. So will "electric \tguitar".

"电吉他".split()会给你['electric', '吉他']。所以将“电\ tguitar”。

#5


-1  

This function removes everything which is not digit in a string. I use it all over the place.

此函数删除字符串中非数字的所有内容。我到处都用。

def parseInt(string):
    if isinstance(string, (str, int, unicode)):
        try:
            digit = int(''.join([x for x in string if x.isdigit() ]))
        except ValueError:
            return False
        else:
            return digit
    else:
        return False   

#6


-9  

There could be many white spaces like below:

可能有很多空格,如下所示:

var = "         This      is the example  of how to remove spaces   "

Just do simple task like, use replace function:

只需做简单的任务,如使用替换功能:

realVar = var.replace(" ",'')

Now the outpur would be:

恩特普尔人会说:

Thisistheexampleofhowtoremovespaces 

Just Chill......... :-)

只是寒冷.........:-)

#1


51  

Split on any whitespace, then join on a single space.

在任何空格上拆分,然后加入一个空格。

' '.join(s.split())

#2


20  

>>> import re
>>> re.sub(r'\s+', ' ', 'some   test with     ugly  whitespace')
'some test with ugly whitespace'

#3


7  

I would use Django's slugify method, which condenses spaces into a single dash and other helpful features:

我将使用Django的slugify方法,将空间压缩为一个单一的dash和其他有用的特性:

from django.template.defaultfilters import slugify

#4


1  

"electric guitar".split() will give you ['electric', 'guitar']. So will "electric \tguitar".

"电吉他".split()会给你['electric', '吉他']。所以将“电\ tguitar”。

#5


-1  

This function removes everything which is not digit in a string. I use it all over the place.

此函数删除字符串中非数字的所有内容。我到处都用。

def parseInt(string):
    if isinstance(string, (str, int, unicode)):
        try:
            digit = int(''.join([x for x in string if x.isdigit() ]))
        except ValueError:
            return False
        else:
            return digit
    else:
        return False   

#6


-9  

There could be many white spaces like below:

可能有很多空格,如下所示:

var = "         This      is the example  of how to remove spaces   "

Just do simple task like, use replace function:

只需做简单的任务,如使用替换功能:

realVar = var.replace(" ",'')

Now the outpur would be:

恩特普尔人会说:

Thisistheexampleofhowtoremovespaces 

Just Chill......... :-)

只是寒冷.........:-)