def count_conditions(input_str, listbox):
electric_attributes = ['electric', 'batter', "power"]
input_str = input_str.strip().replace('rn', 'n').replace('r', 'n')
pattern = re.compile(r'({AND.*?})', re.DOTALL)
matches = re.findall(pattern, input_str)
line_num = 1
for match in matches:
attrs_found = []
words = re.findall(r'bw+b', match)
for word in words:
for attr in electric_attributes:
if attr in word.lower() and word not in attrs_found:
attrs_found.append(word)
if attrs_found:
message = "{} electrical attribute(s) found on lines {}-{} of AND condition: {}".format(
len(attrs_found),
line_num,
line_num + len(match.splitlines()) - 1,
', '.join(attrs_found)
)
else:
message = "No electrical attribute(s) found on lines {}-{} of AND condition".format(
line_num,
line_num + len(match.splitlines()) - 1,
)
listbox.insert("end", message)
line_num += match.count('n') + 1
我希望代码能够确定外部"one_answers">
例如,第1-14行应该被视为1 AND语法,而不是3。
{AND
condition_1:batterie
condition_2:samsung
{OR
{AND
condition_1:TV
condition_2:LCD
}
{AND
condition_1:remote
condition_2:TV
}
}
}
{AND
condition_1:batterie
condition_2:samsung
{OR
condition_3:TV
condition_4:remote
}
}
应该是:
1 electrical attribute found on lines: 1-14 of AND condition: batterie
1 electric attribute found on lines 15-22 of AND condition: batterie.
请帮忙!我做错了什么?或者其他方法?
我根据我的注释写了一些代码(我也对你的注释做了一些更改,打印这些行而不是将它们插入未知的listbox
对象中,并用batterie
的全名更新electric_attributes
)。
def count_conditions(input_str):
electric_attributes = ['electric', 'batterie', "power"]
input_lines = [line for line in input_str.split('n') if line]
open_brackets = 0
for i, line in enumerate(input_lines):
if open_brackets == 0:
# Start of a new outer 'AND' block
if '{AND' in line:
open_brackets = line.count('{')
current_block = line
block_start = i + 1
continue
else:
# this shouldn't actually happen! But if it does, ignore the line
continue
current_block += line
if '{' in line:
open_brackets += line.count('{')
if '}' in line:
open_brackets -= line.count('}')
if open_brackets == 0:
# We've reached the end of the current external 'AND' block
attrs_found = [attr for attr in electric_attributes if attr in current_block]
if attrs_found:
l = len(attrs_found)
print(f'{l} electrical attribute{"s" if l > 1 else ""} found on lines {block_start}-{i + 1}'
f' of AND condition: {" ,".join(attrs_found)}')
else:
print(f'No electrical attribute found on lines {block_start}-{i + 1}'
f' of AND condition')
例子test='''
{AND
condition_1:batterie
condition_2:samsung
{OR
{AND
condition_1:TV
condition_2:LCD
}
{AND
condition_1:remote
condition_2:TV
}
}
}
{AND
condition_1:batterie
condition_2:samsung
{OR
condition_3:TV
condition_4:remote
}
}
'''
count_conditions(test)
# 1 electrical attribute found on lines 1-14 of AND condition: batterie
# 1 electrical attribute found on lines 15-22 of AND condition: batterie