所以我有以下 .msg 文件的摘录。我想要做的是对于所有 [sel xxx xxx] 标题找到它们,然后阅读它们下面的行。如果任何答案包含 (+3) 或任何 (+x),则将其从消息中删除。之后我想比较每个 [sel xxx xxx] 块,对于具有最高 (+x) 的块,我想在该行的初始标签之后但在文本之前添加另一个标签 [clr 4]。如果有两个答案相等 (+x),那么我希望将 [clr 4] 添加到这两行。
[msg MSG_015_0_0 [Makoto]]
[s][bup 0 6 0 65535 1][f 4 10 65535 0 0][vp 8 0 0 0 65535 0]Dummy[n][f 1 3 65535][w][e]
[sel SEL_016_0_0 top]
[s]A(+3)[e]
[s]B[e]
[s]C(+3)[e]
[msg MSG_021_0_0 [Makoto]]
[s][bup 0 6 0 65535 1][f 4 10 65535 0 0][vp 8 0 0 0 65535 0]Sorry I had to ask something so[n]unreasonable... I appreciate you[n]going along with it, though.[n][f 1 3 65535][w][e]
[sel SEL_023_0_0 top]
[s]Fist! Of! Justice![e]
[s]Report him to the police.[e]
[s][f 0 8 2 1 2217]Try to reach out to her.(+3)[clr 27][e]
所以输出应该是这样的:
[msg MSG_015_0_0 [Makoto]]
[s][bup 0 6 0 65535 1][f 4 10 65535 0 0][vp 8 0 0 0 65535 0]Dummy[n][f 1 3 65535][w][e]
[sel SEL_016_0_0 top]
[s][clr 4]A[e]
[s]B[e]
[s][clr 4]C[e]
[msg MSG_021_0_0 [Makoto]]
[s][bup 0 6 0 65535 1][f 4 10 65535 0 0][vp 8 0 0 0 65535 0]Sorry I had to ask something so[n]unreasonable... I appreciate you[n]going along with it, though.[n][f 1 3 65535][w][e]
[sel SEL_023_0_0 top]
[s]Fist! Of! Justice![e]
[s]Report him to the police.[e]
[s][f 0 8 2 1 2217][clr 4]Try to reach out to her.[clr 27][e]
我今天一直在尝试编写不同的代码片段,但我对编码非常缺乏经验,而且我永远无法真正让它正常运行并获得所需的结果。我尝试通过以下方法有条不紊地解决它:
这是我的尝试:
import re
def process_block(block):
# Find all instances of `(+x)` and their positions in the block
plus_x_matches = re.findall(r'\(\+(\d+)\)', block)
if plus_x_matches:
# Convert the found matches to integers and find the maximum
max_x = max(map(int, plus_x_matches))
# Replace all `(+x)` with an empty string
block = re.sub(r'\(\+\d+\)', '', block)
# Find the position where the maximum `(+x)` was found
max_x_position = block.rfind(f'(+{max_x})')
# If `(+x)` was found, insert `[col 4]` before the text
if max_x_position != -1:
# Insert `[col 4]` at the position of the max `(+x)`
block = block[:max_x_position] + '[col 4]' + block[max_x_position:]
return block
def main():
# Read input file
with open('input.msg', 'r') as infile:
content = infile.read()
# Split the content into blocks
blocks = re.split(r'(\[sel .+? top\])', content)
# Process each block
result = []
in_block = False
current_block = ''
for part in blocks:
if part.startswith('[sel'):
# If there's a previous block to process, do so
if in_block:
result.append(process_block(current_block))
current_block = ''
# Start a new block
result.append(part)
in_block = True
elif in_block:
# Append to the current block content
current_block += part
# Don't forget to process the last block
if in_block:
result.append(process_block(current_block))
# Write output file
with open('output.msg', 'w') as outfile:
outfile.write(''.join(result))
if __name__ == '__main__':
main()
我获取了代码库并添加了以新顺序复制和替换的逻辑。
使用多个捕获组构建了一个新模式,以获取每场比赛的内容并将其重新排列成具有所请求的替换的新文本。
此代码可以在终端或jupyter笔记本中运行。
import re
content ="""
[msg MSG_015_0_0 [Makoto]]
[s][bup 0 6 0 65535 1][f 4 10 65535 0 0][vp 8 0 0 0 65535 0]Dummy[n][f 1 3 65535][w][e]
[sel SEL_016_0_0 top]
[s]A(+3)[e]
[s]B[e]
[s]C(+3)[e]
[msg MSG_021_0_0 [Makoto]]
[s][bup 0 6 0 65535 1][f 4 10 65535 0 0][vp 8 0 0 0 65535 0]Sorry I had to ask something so[n]unreasonable... I appreciate you[n]going along with it, though.[n][f 1 3 65535][w][e]
[sel SEL_023_0_0 top]
[s]Fist! Of! Justice![e]
[s]Report him to the police.[e]
[s][f 0 8 2 1 2217]Try to reach out to her.(+3)[clr 27][e]
"""
def process_block(block):
# Find all instances of `(+x)` and their positions in the block
plus_x_matches = re.findall(r'\(\+(\d+)\)', block)
print('block')
print(block[:25])
print('plus_x_matches')
print(plus_x_matches)
if plus_x_matches:
# Convert the found matches to integers and find the maximum
max_x = max(map(int, plus_x_matches))
patter_find_replace = r'\[s\](\[.*?\])?(.*)(\(\+'+str(max_x)+r'\))(\[.*?\])?\[e\]'
new_lines = []
lines = block.split('\n')
for l in lines :
pattern = re.compile(patter_list_content)
matches = pattern.findall(l)
if(len(matches)>0):
item = matches[0]
replaced = '[s]'+item[0]+'[col 4]'+item[1]+item[3]+'[e]'
new_lines.append(replaced)
else:
new_lines.append(l)
block = "\n".join(new_lines)
return block
def main():
# Split the content into blocks
blocks = re.split(r'(\[sel .+? top\])', content)
# Process each block
result = []
in_block = False
current_block = ''
for part in blocks:
if part.startswith('[sel'):
# If there's a previous block to process, do so
if in_block:
result.append(process_block(current_block))
current_block = ''
# Start a new block
result.append(part)
in_block = True
elif in_block:
# Append to the current block content
current_block += part
# Don't forget to process the last block
if in_block:
result.append(process_block(current_block))
print('result:')
for r in result:
print(r)
main()