Home > Back-end >  Text File Manipulation using For Loop
Text File Manipulation using For Loop

Time:11-25

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

CodePudding user response:

You can use itertools.groupby:

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

from itertools import groupby

for _, g in groupby(text.splitlines(), lambda l: "#" in l):
    print("".join(g))

Prints:

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

To load text from your file you can use:

with open('my_file.txt', 'r') as f_in:
    text = f_in.read()

CodePudding user response:

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)

CodePudding user response:

I think you can use lines = f.read().splitlines() instead of f.readlines() and in your for loop, you can do something like

for l in lines
    tmp = ""
    if '#' in l:
        print(tmp)
        tmp = ""
        print(l)
    else:
        tmp =l
    ```
  • Related