How can I use Python's readlines function to format lines from a file in a specific pattern?

68 Views Asked by At

The data I have in the test.txt file looks like this:

XXTYSVASOOXOSJAY
CGTVHIXXHIAICXWHAYX

and I'm trying to achieve a pattern like this:

XX-X
XX-X-X

This is what I have so far:

import re

data = open("test.txt", "r")
lines = data.readlines()

result = re.sub(r"[^X]+", r"-", str(lines)).strip("-")
if "X" in result:
  print(result)
else:
  print("No X found")

This is the result I get, it's a single line: XX-X-XX-X-X.

How can I do this correctly to get the expected result?

1

There are 1 best solutions below

0
sedub01 On

To format lines from a file with a specific pattern, you can iterate over each line in the file and apply regular expression substitution to each line apart. For example

import re

with open("test.txt", "r") as f:
    for line in f:
        result = re.sub(r"[^X]+", r"-", line).strip("-")
        print(result if "X" in result else "No X found")