我正在尝试删除 Linux 上 python 2.7 中的所有空格/制表符/换行符。
我写了这个,应该可以完成这项工作:
myString="I want to Remove all white \t spaces, new lines \n and tabs \t"
myString = myString.strip(' \n\t')
print myString
output:
I want to Remove all white spaces, new lines
and tabs
这似乎是一件简单的事情,但我在这里遗漏了一些东西。我应该进口一些东西吗?
Use str.split([sep[, maxsplit]])
没有sep
or sep=None
:
From docs:
If sep
未指定或者是None
,不同的分割算法是
应用:连续的空白被视为单个
分隔符,结果开头不包含空字符串
如果字符串有前导或尾随空格,则结束。
Demo:
>>> myString.split()
['I', 'want', 'to', 'Remove', 'all', 'white', 'spaces,', 'new', 'lines', 'and', 'tabs']
Use str.join
在返回的列表上获取此输出:
>>> ' '.join(myString.split())
'I want to Remove all white spaces, new lines and tabs'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)