在单词AND和OR上分割字符串,并保持分隔符

时间:2019-12-08 11:56:25

标签: python list split

我想分割以下字符串

string = "events.error > 5 AND timeseries.temperature < 20 OR variants.battery = 'Li' AND vehicle.name = 'Audi'"

位于AND和OR位置,以便我可以返回以下列表

list = ["events.error > 5", "AND", "timeseries.temperature < 20", "OR", "variants.battery = 'Li'", "AND", "vehicle.name = 'Audi'"]

我尝试过string.split(),但组也被空格分开。您知道更好的方法吗?

4 个答案:

答案 0 :(得分:1)

您可以split()上的字符串"AND",然后在列表中的各项之间插入"AND。似乎您也不需要任何尾随空格,因此也需要strip()

string = "events.error > 5 AND timeseries.temperature < 20 AND variants.battery = 'Li' AND vehicle.name = 'Audi'"

def intersperse(lst, item):
    result = [item] * (len(lst) * 2 - 1)
    result[0::2] = lst
    return result

result = intersperse([x.strip() for x in string.split("AND")], "AND")

print(result)

输出:

['events.error > 5', 'AND', 'timeseries.temperature < 20', 'AND', "variants.battery = 'Li'", 'AND', "vehicle.name = 'Audi'"]

答案 1 :(得分:1)

最简单的方法是使用re.split,将分隔符放在一组中将使其保留在输出中:

import re

string = "events.error > 5 AND timeseries.temperature < 20 AND variants.battery = 'Li' AND vehicle.name = 'Audi'"

re.split(r'(AND)', string)

输出:

['events.error > 5 ',
 'AND',
 ' timeseries.temperature < 20 ',
 'AND',
 " variants.battery = 'Li' ",
 'AND',
 " vehicle.name = 'Audi'"]

编辑:当您包含第二个分隔符“或”时,更新版本:

import re

string = "events.error > 5 AND timeseries.temperature < 20 OR variants.battery = 'Li' AND vehicle.name = 'Audi'"

re.split(r'(AND|OR)', string)

输出:

['events.error > 5 ',
 'AND',
 ' timeseries.temperature < 20 ',
 'OR',
 " variants.battery = 'Li' ",
 'AND',
 " vehicle.name = 'Audi'"]

如果要消除字符串周围的空格,可以在分隔符中但在组之外包含任意数量的前导和尾随空格,这样它们就不会出现在输出中:

import re

string = "events.error > 5 AND timeseries.temperature < 20 OR variants.battery = 'Li' AND vehicle.name = 'Audi'"

re.split(r'\s*(AND|OR)\s*', string)  # include the spaces in the separator, 
                                     # but don't keep them 

输出:

['events.error > 5',
 'AND',
 'timeseries.temperature < 20',
 'OR',
 "variants.battery = 'Li'",
 'AND',
 "vehicle.name = 'Audi'"]

答案 2 :(得分:0)

您可以先用string.split("AND")分割字符串,再用.strip()分割字符串,以丢失前导/尾随空格:

my_string = "events.error > 5 AND timeseries.temperature < 20 AND variants.battery = 'Li' AND vehicle.name = 'Audi'" 
my_list = my_string.split('AND') 
my_list = [x.strip() for x in my_list]
print(my_list)

返回

['events.error > 5', 'timeseries.temperature < 20', "variants.battery = 'Li'", "vehicle.name = 'Audi'"]

请注意,如果没有.strip(),则会保留不必要的空白,而这可能是您不希望的。

答案 3 :(得分:0)

您可以将split()itertools.cycle()zip()组合:

import itertools


string = "events.error > 5 AND timeseries.temperature < 20 AND variants.battery = 'Li' AND vehicle.name = 'Audi'"
print([x for y in zip(string.split(' AND '), itertools.cycle(['AND'])) for x in y][:-1])
# ['events.error > 5', 'AND', 'timeseries.temperature < 20', 'AND', "variants.battery = 'Li'", 'AND', "vehicle.name = 'Audi'"]