【问题标题】:Remove whitespace in Python using string.whitespace使用 string.whitespace 在 Python 中删除空格
【发布时间】:2010-12-26 07:12:33
【问题描述】:

Python 的 string.whitespace 很棒:

>>> string.whitespace
'\t\n\x0b\x0c\r '

如何在不手动输入 '\t|\n|... 等正则表达式的情况下将其与字符串一起使用?

例如,它应该能够转向: “请\n不要\t伤害\x0b我。”

进入

“请不要伤害我。”

我可能想保留单个空格,但我想只要去 string.whitespace[:-1] 就很容易了。

【问题讨论】:

    标签: python string whitespace


    【解决方案1】:

    这个用例有一个特殊的快捷方式!

    如果您在没有参数的情况下调用 str.split,它会在运行的空白处而不是单个字符上进行拆分。所以:

    >>> ' '.join("Please \n don't \t hurt \x0b me.".split())
    "Please don't hurt me."
    

    【讨论】:

    • 这比我的解决方案要好得多。我也希望有朝一日长生不老。
    • 哇。这是惊人的。非常适合我正在做的事情,因为它们是小字符串。我想知道这将如何在大型数据集上执行?如果有人知道它本质上是如何工作的,那就太好了:)
    • 谢谢,不知道对空格运行不使用任何参数。好大!!
    • 这仍然比 20MB 字符串的正则表达式快。
    • @Dominique: 是的,这是一个documented stdlib 功能——“如果 sep 未指定或为无,则应用不同的分割算法......”——这已被广泛使用,但不太可能已弃用。
    【解决方案2】:

    \s 字符类有什么问题?

    >>> import re
    
    >>> pattern = re.compile(r'\s+')
    >>> re.sub(pattern, ' ', "Please \n don't \t hurt \x0b me.")
    "Please don't hurt me."
    

    【讨论】:

    • 没什么,很好的解决方案。我认为 .join/split 选项非常简洁,你不觉得吗? :)
    • 确实如此。事实上,timeit 显示 join/split 比给定字符串的 re.sub() 快 6 倍。
    • 我想一旦编译并sub 重复使用多次,这也可能很快
    【解决方案3】:

    让我们做一些合理的假设:

    (1) 您确实想用一个空格替换任何一行空白字符(一行长度为 1 或更大)。

    (2) 您希望相同的代码在 Python 2.X 下使用 unicode 对象进行最小的更改。

    (3) 你不希望你的代码假设文档中没有保证的东西

    (4) 您希望使用相同的代码对 Python 3.X str 对象进行最小的更改。

    当前选择的答案有这些问题:

    (a) 将" " * 3 更改为" " * 2,即它删除了重复的空格,但不删除一式三份、四份等空格。 [不符合要求 1]

    (b) 将 "foo\tbar\tzot" 更改为 "foobarzot" [不符合要求 1]

    (c) 当输入一个 unicode 对象时,得到 TypeError: translate() takes exactly one argument (2 given) [不符合要求 2]

    (d) 使用string.whitespace[:-1] [不符合要求 3; string.whitespace 中的字符顺序无法保证]

    (e) 使用string.whitespace[:-1] [不符合要求 4;在 Python 2.X 中,string.whitespace 为 '\t\n\x0b\x0c\r ';在 Python 3.X 中,它是 ' \t\n\r\x0b\x0c']

    " ".join(s.split()) 答案和re.sub(r"\s+", " ", s) 答案没有这些问题。

    【讨论】:

    • 嘿,你提出了一些很好的观点。对我来说,' '.join(s.split()) 适用于“foo\tbar\tzot”测试!我的意思是,最初的答案对我有用,但这只是因为我没想到会有这么奇怪的字符串。然而,处理这个问题的东西会很棒。我刚刚用 "foo\tbar\tzot" 测试了 sub 并且它可以工作......所以我想我只是选择 ' '.join(s.split()) 版本,因为它很简单并且能够在没有的情况下工作导入 re 模块。而且我的数据集很小,所以我不担心性能问题(如果有的话)。
    【解决方案4】:

    你可以使用翻译方法

    import string
    
    s = "Please \n don't \t hurt \x0b me."
    s = s.translate(None, string.whitespace[:-1]) # python 2.6 and up
    s = s.translate(string.maketrans('',''), string.whitespace[:-1]) # python 2.5, dunno further down
    >>> s
    "Please  don't  hurt  me."
    

    然后删除重复的空格

    s.replace('  ', ' ')
    >>> s
    "Please don't hurt me."
    

    【讨论】:

    • 查看编辑。另外,您使用的是哪个python版本?您需要 2.6 才能使 None 参数起作用。
    • 是的,我正在使用 2.5... 有没有替代方案?否则我将不得不使用其他答案...
    • 很好,非常感谢!这是现在最好的答案,特别是因为它适合我的 2.5 岁。
    【解决方案5】:

    一个起点..(虽然它不比手动组装空白马戏团短)..

    >>> from string import whitespace as ws
    >>> import re
    
    >>> p = re.compile('(%s)' % ('|'.join([c for c in ws])))
    >>> s = "Please \n don't \t hurt \x0b me."
    
    >>> p.sub('', s)
    "Pleasedon'thurtme."
    

    或者如果您想将空格减少到最多一个:

    >>> p1 = re.compile('(%s)' % ('|'.join([c for c in ws if not c == ' '])))
    >>> p2 = re.compile(' +')
    >>> s = "Please \n don't \t hurt \x0b me."
    
    >>> p2.sub(' ', p1.sub('', s))
    "Please don't hurt me."
    

    第三种方式,更紧凑:

    >>> import string
    
    >>> s = "Please \n don't \t hurt \x0b me."
    >>> s.translate(None, string.whitespace[])
    "Pleasedon'thurtme."
    
    >>> s.translate(None, string.whitespace[:5])
    "Please  don't  hurt  me."
    
    >>> ' '.join(s.translate(None, string.whitespace[:5]).split())
    "Please don't hurt me."
    

    【讨论】:

    • 我最初有这个作为第一个答案;这是一个很好的解决方案,并且很好地利用了 python 的简单性:)
    猜你喜欢
    • 2016-08-25
    • 2013-06-25
    • 2013-09-19
    • 2013-10-10
    • 2018-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    相关资源
    最近更新 更多