Python / Django:如何删除多余的空格&标签从字符串? [英] Python/Django: How to remove extra white spaces & tabs from a string?

查看:293
本文介绍了Python / Django:如何删除多余的空格&标签从字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python / Django构建一个网站。用户提交标签。每个标签可以包含多个单词。每个标签都有一个ID号。我想要确保格式略有不同的标签仍被认为是相同的标签。



例如,如果一个用户提交了标签电吉他和其他提交的电吉他(2个单词之间的2个空格)我想要能够识别他们是相同的标签。



如何删除所有在这种情况下,额外的空白和标签?谢谢。

解决方案

分割任何空格,然后加入一个空格。

 ''.join(s.split())


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.

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.

解决方案

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

' '.join(s.split())

这篇关于Python / Django:如何删除多余的空格&标签从字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆