剥离空格/标签/换行符 - python

时间:2012-05-22 22:37:01

标签: python string strip

我试图在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

输出:

I want to Remove all white   spaces, new lines 
 and tabs

这似乎很简单,但我在这里缺少一些东西。我应该导入一些东西吗?

8 个答案:

答案 0 :(得分:101)

使用str.split([sep[, maxsplit]])时没有sepsep=None

来自docs

  

如果未指定sepNone,则使用不同的拆分算法   applied:连续空格的运行被视为单个   分隔符,结果将在开始时不包含空字符串   或者如果字符串有前导或尾随空格则结束。

<强>演示:

>>> myString.split()
['I', 'want', 'to', 'Remove', 'all', 'white', 'spaces,', 'new', 'lines', 'and', 'tabs']

在返回的列表中使用str.join来获取此输出:

>>> ' '.join(myString.split())
'I want to Remove all white spaces, new lines and tabs'

答案 1 :(得分:39)

如果要删除多个空格项并用单个空格替换它们,最简单的方法是使用这样的正则表达式:

>>> import re
>>> myString="I want to Remove all white \t spaces, new lines \n and tabs \t"
>>> re.sub('\s+',' ',myString)
'I want to Remove all white spaces, new lines and tabs '

如果您愿意,可以使用.strip()删除尾随空格。

答案 2 :(得分:10)

import re

mystr = "I want to Remove all white \t spaces, new lines \n and tabs \t"
print re.sub(r"\W", "", mystr)

Output : IwanttoRemoveallwhitespacesnewlinesandtabs

答案 3 :(得分:6)

使用重新

import re
myString = "I want to Remove all white \t spaces, new lines \n and tabs \t"
myString = re.sub(r"[\n\t\s]*", "", myString)
print myString

<强>输出:

  

IwanttoRemoveallwhitespaces,newlinesandtabs

答案 4 :(得分:1)

  

这只会删除标签页,换行符,空格,而不会删除任何内容。

import re
myString = "I want to Remove all white \t spaces, new lines \n and tabs \t"
output   = re.sub(r"[\n\t\s]*", "", myString)

输出:

  

IwantoRemoveallwhiespaces,newlinesandtabs

美好的一天!

答案 5 :(得分:1)

上述建议使用正则表达式的解决方案并不理想,因为这是一个很小的任务,并且正则表达式需要更多的资源开销,而不是任务的简单性所能证明的。

这就是我要做的:

myString = myString.replace(' ', '').replace('\t', '').replace('\n', '')

或者如果您要删除一堆东西,那么单行解决方案将很长:

removal_list = [' ', '\t', '\n']
for s in removal_list:
  myString = myString.replace(s, '')

答案 6 :(得分:0)

由于没有其他更复杂的内容了,因此我想分享一下,因为它对我有帮助。

这是我最初使用的:

import requests
import re

url = 'https://stackoverflow.com/questions/10711116/strip-spaces-tabs-newlines-python' # noqa
headers = {'user-agent': 'my-app/0.0.1'}
r = requests.get(url, headers=headers)
print("{}".format(r.content))

不良结果:

b'<!DOCTYPE html>\r\n\r\n\r\n    <html itemscope itemtype="http://schema.org/QAPage" class="html__responsive">\r\n\r\n    <head>\r\n\r\n        <title>string - Strip spaces/tabs/newlines - python - Stack Overflow</title>\r\n        <link

这就是我将其更改为:

import requests
import re

url = 'https://stackoverflow.com/questions/10711116/strip-spaces-tabs-newlines-python' # noqa
headers = {'user-agent': 'my-app/0.0.1'}
r = requests.get(url, headers=headers)
regex = r'\s+'
print("CNT: {}".format(re.sub(regex, " ", r.content.decode('utf-8'))))

所需结果:

<!DOCTYPE html> <html itemscope itemtype="http://schema.org/QAPage" class="html__responsive"> <head> <title>string - Strip spaces/tabs/newlines - python - Stack Overflow</title>

@MattH提到的精确正则表达式是使我适合我的代码的方法。谢谢!

注意:这是python3

答案 7 :(得分:0)

在联接中使用列表推导的单行代码怎么样?

library(tidyr)
library(ggplot2)

# (calling your data d)
d %>%
  # widen the data so each plot dimension gets a column
  pivot_wider(names_from = Year, values_from = Number) %>%
  # use backticks for non-standard column names (because of the dash in this case)
  ggplot(aes(x = `1985-99`, y = `2000-14`, color = airline)) +
  geom_point()
相关问题