在python中删除字符串中的空格和制表符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21966556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:59:18  来源:igfitidea点击:

Remove spaces and tabs within a string in python

pythonstringpython-2.7

提问by user27

How can I remove specific whitespace within a string in python.

如何在 python 中删除字符串中的特定空格。

My input string is:,

我的输入字符串是:,

str1 = """vendor_id\t: GenuineIntel
        cpu family\t: 6
        model\t\t: 58
        model name\t: Intel(R) Core(TM) i3-3120M CPU @ 2.50GHz
        stepping\t: 9
        cpu MHz\t\t: 2485.659
        cache size\t: 6144 KB
        fpu\t\t: yes
        fpu_exception\t: yes
        cpuid level\t: 5
        wp\t\t: yes"""

My required output is:

我需要的输出是:

>>>print str1
vendor_id: GenuineIntel
cpu family: 6
model: 58
model name: Intel(R) Core(TM) i3-3120M CPU @ 2.50GHz
stepping: 9
cpu MHz: 2485.659
cache size: 6144 KB
fpu: yes
fpu_exception: yes
cpuid level: 5
wp: yes

回答by jonrsharpe

I don't know what you mean by "randomly", but you can remove all tabs with:

我不知道你所说的“随机”是什么意思,但你可以删除所有标签:

str1 = str1.replace("\t", "")

回答by Lalit Sachdeva

str1 = str1.replace("\t", "").replace(" ", "")

it would replace the tabs first and then white spaces.

它会先替换制表符,然后替换空格。

回答by Martijn Pieters

Looks like you want to remove the whitespace from the start of lines, and remove all whitespace before a colon. Use regular expressions:

看起来您想从行首删除空格,并删除冒号前的所有空格。使用正则表达式:

import re

re.sub(r'(^[ \t]+|[ \t]+(?=:))', '', str1, flags=re.M)

This picks out spaces and tabs at the start of lines (^[ \t]*, ^is the start of a line, [ \t]is a space or tab, +is 1 or more), orit picks out spaces and tabs right before a colon ([ \t]+is 1 or more spaces and tabs, (?=:)means that a :character must follow but isn't included in what is picked) and then replaces those spaces and tabs with an empty string. The flags=re.Mis there to make sure the pattern works on each individual line.

这会在行首(^[ \t]*,^是行的开头,[ \t]是空格或制表符,+是 1 个或更多),或者在冒号之前选择空格和制表符([ \t]+是 1 个或多个空格和制表符)tabs,(?=:)意味着一个:字符必须跟在后面但不包含在选择的内容中),然后用空字符串替换这些空格和制表符。该flags=re.M是有确保每个单独的行模式的作品。

Demo:

演示:

>>> import re
>>> str1 = """vendor_id\t: GenuineIntel
...         cpu family\t: 6
...         model\t\t: 58
...         model name\t: Intel(R) Core(TM) i3-3120M CPU @ 2.50GHz
...         stepping\t: 9
...         cpu MHz\t\t: 2485.659
...         cache size\t: 6144 KB
...         fpu\t\t: yes
...         fpu_exception\t: yes
...         cpuid level\t: 5
...         wp\t\t: yes"""
>>> print re.sub(r'(^[ \t]+|[ \t]+(?=:))', '', str1, flags=re.M)
vendor_id: GenuineIntel
cpu family: 6
model: 58
model name: Intel(R) Core(TM) i3-3120M CPU @ 2.50GHz
stepping: 9
cpu MHz: 2485.659
cache size: 6144 KB
fpu: yes
fpu_exception: yes
cpuid level: 5
wp: yes

If your input string does nothave leading whitespace (and you just indented your sample yourself to make it look lined up), then all you want to remove is tabs:

如果你输入的字符串并不会有前导空格(和你只是你自己缩进你的样品,使它看起来一字排开),那么所有要删除的选项卡:

str1 = str1.replace('\t', '')

and be done with it.

并完成它。

回答by adnanalhomssi

This will solve your answer:

这将解决您的答案:

str1 = """vendor_id\t: GenuineIntel
    cpu family\t: 6
    model\t\t: 58
    model name\t: Intel(R) Core(TM) i3-3120M CPU @ 2.50GHz
    stepping\t: 9
    cpu MHz\t\t: 2485.659
    cache size\t: 6144 KB
    fpu\t\t: yes
    fpu_exception\t: yes
    cpuid level\t: 5
    wp\t\t: yes"""
arr = [line.strip() for line in str1.split('\n')]
for line in arr:
    print line.strip()