Search code examples
pythonstringfiletext

Text File Manipulation using For Loop


I have a text file that looks like this:

line1                #commentA
line2
line3                #commentB
line4
line5
line6                #commentC
line7
line8
line9
line10
line11               #commentD
line12

I want to reformat it to look like this:

line1                #commentA
line2
line3                #commentB
line4line5
line6                #commentC
line7line8line9line10
line11               #commentD
line12

The number of lines between lines that have comments is variable, and can be large (up to 1024 lines). So I'm looking for a way to leave the line with the comment untouched, but append the text of all lines between lines with comments into one line. Can you give a suggestion?

My start at this is as follows:

with open("myfile.txt", mode='r+') as f:
    lines = f.readlines()

for n, content in enumerate(lines):
    lines[n] = content
    if '#' in lines[n]:
        print(lines[n])
# not sure how to combine the lines in between those with comments

Solution

  • One way to do it :

    new_lines = []
    for line in lines:
        if "#" in line:
            new_lines.append(f"\n{line}\n")
        else:
            new_lines.append(line)
    

    It will gives you another list of lines, than you can then save to a new file with this (note the "".join because the lines already contains the \n) :

    with open('file.txt', 'w') as fo:
        fo.write("".join(new_lines))
    

    However, if the first line contains a comment, this will add an extra new line at the beginning of the file. To remove it, you can either check for it after the loop :

    if new_lines[0].startswith("\n"):
        new_lines[0] = new_lines[0][1:]
    

    Or change the loop as such :

    new_lines: list[str] = []
    for index, line in enumerate(lines):
        if "#" in line:
            new_lines.append(("\n" if index > 0 else "") + f"{line}\n")
        else:
            new_lines.append(line)