【问题标题】:Python - defining string split delimiter?Python - 定义字符串拆分分隔符?
【发布时间】:2014-12-10 09:34:33
【问题描述】:

如何定义字符串分隔符以最有效的方式进行拆分?我的意思是不需要使用很多 if 等?

我有需要严格拆分为两个元素列表的字符串。问题是那些字符串有不同的符号,我可以用它们来分割它们。例如:

'Hello: test1'。这个有分割分隔符': '。另一个例子是: 'Hello - test1'。所以这个是' - '。拆分分隔符也可以是' -''- '。因此,如果我知道分隔符的所有变体,我如何才能最有效地定义它们?

首先我做了这样的事情:

strings = ['Hello - test', 'Hello- test', 'Hello -test']
for s in strings:
    delim = ' - '
    if len(s.split('- ', 1)) == 2:
        delim = '- '
    elif len(s.split(' -', 1)) == 2:
        delim = ' -'
    print s.split(delim, 1)[1])

但后来我得到了具有另一个意外分隔符的新字符串。所以这样做我应该添加更多的 if 来检查其他分隔符,比如': '。但是后来我想知道是否有更好的方法来定义它们(如果我以后需要在某种列表中包含新的定界符,这没有问题)。也许正则表达式会有所帮助或其他一些工具?

【问题讨论】:

    标签: python regex string split


    【解决方案1】:

    使用逻辑或| 运算符将所有分隔符放入re.split 函数中,如下所示。

    re.split(r': | - | -|- ', string)
    

    如果您想进行一次性拆分,请添加maxsplit=1

    re.split(r': | - | -|- ', string, maxsplit=1)
    

    【讨论】:

    • 这似乎是最好的定义方式,因为您可以明确定义分隔符是什么。
    • 对不起,我不明白你的意思。
    • 您也可以更新您的答案,在我询问如何拆分为两个元素列表时添加maxsplit=1。因此,在第一次拆分后,它不应该再拆分该字符串。我的意思是,使用其他答案,分隔符可以变成例如:-,即使你不想要它,然后它会错误地拆分。
    【解决方案2】:

    可以使用re模块的split函数

    >>> strings = ['Hello1 - test1', 'Hello2- test2', 'Hello3 -test3', 'Hello4 :test4', 'Hello5 : test5']
    >>> for s in strings:
    ...   re.split(" *[:-] *",s)
    ...
    ['Hello1', 'test1']
    ['Hello2', 'test2']
    ['Hello3', 'test3']
    ['Hello4', 'test4']
    ['Hello5', 'test5']
    

    [] 之间的位置放置了所有可能的分隔符。 * 表示可以在前面或后面放一些空格。

    【讨论】:

      【解决方案3】:
      \s*[:-]\s*
      

      您可以按此拆分。使用re.split(r"\s*[:-]\s*",string)。查看演示。

      https://regex101.com/r/nL5yL3/14

      如果您可以使用 --- 等分隔符,则应使用此分隔符。其中可以有多个空格。

      【讨论】:

        【解决方案4】:

        这不是最好的方法,但如果您出于某种(或没有)原因想要避免使用 re,我会这样做:

        >>> strings = ['Hello - test', 'Hello- test', 'Hello -test', 'Hello : test']
        >>> delims = [':', '-']  # all possible delimiters; don't worry about spaces.
        >>>
        >>> for string in strings:
        ...     delim = next((d for d in delims if d in string), None) # finds the first delimiter in delims that's present in the string (if there is one)
        ...     if not delim:
        ...         continue  # No delimiter! (I don't know how you want to handle this possibility; this code will simply skip the string all together.)
        ...     print [s.strip() for s in string.split(delim, 1)]  # assuming you want them in list form.
        ['Hello', 'test']
        ['Hello', 'test']
        ['Hello', 'test']
        ['Hello', 'test']
        

        这使用 Python 的原生 .split() 在分隔符处断开字符串,然后使用 .strip() 修剪结果中的空白(如果有)。我使用next 来找到合适的分隔符,但有很多东西可以用来交换(特别是如果你喜欢for 块)。

        如果您确定每个字符串将包含至少一个分隔符(最好是完全一个),那么您可以将其缩减为:

         ## with strings and delims defined...
        >>> for string in strings:
        ...     delim = next(d for d in delims if d in string) # raises StopIteration at this line if there is no delimiter in the string.
        ...     print [s.strip() for s in string.split(delim, 1)]
        

        我不确定这是否是最优雅的解决方案,但它使用的 if 块更少,而且您无需导入任何内容即可。

        【讨论】:

        • 如果您使用第一个 for 块示例,并且不知何故要求您使用零作为分隔符,请确保使用字符串 '0' 而不是整数 0,因为整数是 Falsey 并且 if not delim: 将在 delim == 0 时运行。您可能还想将 if not delim: 更改为 if delim is None: (或您将 next 的默认设置设置为的任何内容);总的来说是一个更安全的选择。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-22
        • 2011-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-06
        相关资源
        最近更新 更多