【问题标题】:Remove spaces and tabs within a string in python在python中删除字符串中的空格和制表符
【发布时间】:2014-02-23 09:39:39
【问题描述】:

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

我的输入字符串是:,

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 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

【问题讨论】:

  • 你确定你的输出字符串没有空格吗?
  • 我已将您的示例编辑为 a) 是有效的 Python,并且 b) 用 \t 字符替换其中的现有选项卡,因为这在这里很重要但不可见。如果可以的话,请务必使用print repr(str1) 向我们展示其中真正的内容。

标签: python string python-2.7


【解决方案1】:

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

import re

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

这会在行首挑选空格和制表符(^[ \t]*^ 是行首,[ \t] 是空格或制表符,+ 是 1 个或更多), 它会在冒号之前挑选出空格和制表符([ \t]+ 是 1 个或多个空格和制表符,(?=:) 表示必须跟在后面的 : 字符,但不包含在所选择的内容中)和然后用空字符串替换这些空格和制表符。 flags=re.M 用于确保该模式适用于每一行。

演示:

>>> 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

如果您的输入字符串没有有前导空格(并且您只是自己缩进了样本以使其看起来对齐),那么您想要删除的只是制表符:

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

并完成它。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 如果: 字符之前的前导空格或空格也包含常规空格,这还不够。
    【解决方案3】:

    这将解决您的答案:

    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()
    

    【讨论】:

      【解决方案4】:
      def invitation_ics():
          text = f"""BEGIN:VCALENDAR
      CLASS:PUBLIC
      STATUS:CONFIRMED
          """
      retunr text
      

      不是标签

      BEGIN:VCALENDAR
      CLASS:PUBLIC
      STATUS:CONFIRMED
      

      【讨论】:

        【解决方案5】:
        str1 = str1.replace("\t", "").replace(" ", "")
        

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

        【讨论】:

        • 这会将 all 空白替换为空。应保留冒号后和单词之间的空格。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-18
        • 1970-01-01
        • 1970-01-01
        • 2015-04-21
        • 1970-01-01
        • 1970-01-01
        • 2011-09-21
        相关资源
        最近更新 更多