我正在尝试删除Linux上Python2.7中的所有空格/制表符/换行符。
我写了这篇文章,它应该可以完成这项工作:
myString="I want to Remove all white \t spaces, new lines \n and tabs \t"
myString = myString.strip(' \n\t')
print myString
输出:
I want to Remove all white spaces, new lines
and tabs
这似乎是一件简单的事情,但我在这里错过了一些东西。我应该进口点什么吗?
如果要删除多个空白项并用单个空格替换它们,最简单的方法是使用这样的regexp:
如果愿意,可以用
.strip()
删除尾随空格。使用
str.split([sep[, maxsplit]])
而不使用sep
或sep=None
:来自docs:
演示:
使用返回列表中的
str.join
获取此输出:相关问题 更多 >
编程相关推荐