So I have this excerpt of the .msg file below. What I wish to do is for all the [sel xxx xxx] headers find them then read the lines below them. If any of the answers contain a (+3) or any (+x) then remove this from the message. Afterwards I want to compare each [sel xxx xxx] block and for the one with the highest (+x) I want to add another tag [clr 4] to it after the initial tags on the line but before the text. If there are two answers with equal (+x) then I wish to add [clr 4] to both lines.
[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]
So the output should look like this:
[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]
I’ve been trying to write different pieces of code today but I’m very inexperienced with coding and I can never actually get it to run properly with the desired outcomes. I tried to solve it methodically by:
- Identifying the [sel headers using regex.
- Find any instances of (+x) and removing them
- Prepending [clr 4] before the text of the line with maximum (+x).
- Writing this back to an output file without any formatting changes (i.e. linebreaks stay the same, headers stay intact and it’s mostly unchanged).
This is my attempt:
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()
Medusa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2