使用 python 中的符号拆分字符串

Splitting the string using a symbol in python

我有以下字符串:

my_string = '1) ServerName sn = ProtobufUtil.toServerName(request.getServer());\n2) String msg = "Region server " + sn +\n3)  " reported a fatal error:\\n" + errorText;\n4) LOG.error(msg);'

我需要将该字符串转换为按符号 \n 拆分的列表。所以,列表将是这样的:

my_list = ['1) ServerName sn = ProtobufUtil.toServerName(request.getServer());',
           '2) String msg = "Region server " + sn +',
           '3)  " reported a fatal error:\\n" + errorText;',
           '4) LOG.error(msg);'
          ]

我在我的代码中使用符号 \n 作为拆分器:

my_list = my_string.split("\n")

但是,列表中第三个元素的输出与我预期的不同。 输出:

my_list = ['1) ServerName sn = ProtobufUtil.toServerName(request.getServer());',
           '2) String msg = "Region server " + sn +',
           '3)  " reported a fatal error:\',
           '" + errorText;',
           '4) LOG.error(msg);']

拆分器在代码中应该如何定义?

除了正则表达式选项,您别无选择。您可以使用 re.split 和负面回顾来做到这一点。

>>> import re
>>> re.split(r'(?<!\)\n', my_string)

[
    '1) ServerName sn = ProtobufUtil.toServerName(request.getServer())',
    '2) String msg = "Region server " + sn ',
    '3)  " reported a fatal error:\\n" + errorText',
    '4) LOG.error(msg);'
]

lookbehind 指定拆分必须仅在 \n 前面没有更多反斜杠时发生。

你可以试试这个模式,它是 Positive Lookahead :

pattern r'\n(?=\d)'

代码:

my_string = '1) ServerName sn = ProtobufUtil.toServerName(request.getServer());\n2) String msg = "Region server " + sn +\n3)  " reported a fatal error:\\n" + errorText;\n4) LOG.error(msg);'

import re

for i in re.split(r'\n(?=\d)',my_string):
    print(i)

输出:

1) ServerName sn = ProtobufUtil.toServerName(request.getServer());
2) String msg = "Region server " + sn +
3)  " reported a fatal error:\n" + errorText;
4) LOG.error(msg);