-3

I would like to add a new line to a file after findall finds a search pattern. The code I use only writes the content of the input file to the output file. It doesn't add new line to the output file. How can I fix my code?

import re
text = """
Hi! How are you?
Can you hear me?
"""
with open("input.txt", "r") as infile:
    readcontent = infile.readlines()

with open("output.txt", "w") as out_file:
    for line in readcontent:
    x1 = re.findall(text, line)
    if line == x1:
        line = line + text
    out_file.write(line)

Input.txt:

ricochet robots
settlers of catan
acquire
Hi! How are you?
Can you hear me?
this is very valuable
finish

Desired output.txt:

ricochet robots
settlers of catan
acquire
Hi! How are you?
Can you hear me?

Added new line

this is very valuable
finish
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
erhan
  • 317
  • 1
  • 6
  • 16
  • 1
    Essentially, you need a new line after every 'Can you hear me?' ? – Austin Jul 02 '18 at 06:36
  • There is only one line "Can you hear me?" in the input file. So I need only a new line after that. – erhan Jul 02 '18 at 06:39
  • One line or many, `regex` seems an overkill here. – Austin Jul 02 '18 at 06:41
  • @klutt the actual output is something like the desired output. As I said before, I need to use `findall` to cacth multiple lines in the input file. The actual input is more complex. – erhan Jul 02 '18 at 07:28

3 Answers3

1

Use no regex here. Check current line, if it's the line to be checked, add a newline.

with open("output.txt", "w") as out_file:
    for line in readcontent:
        out_file.write(line)
        if line.strip() == 'Can you hear me?':
            out_file.write('\n')

If you need a regex itself, go for below (though I would never recommend):

with open("output.txt", "w") as out_file:
    for line in readcontent:
        out_file.write(line)
        if re.match('Can you hear me?', line.strip()):
            out_file.write('\n')
Austin
  • 25,759
  • 4
  • 25
  • 48
0

Try iterating over each line and check if your text exist.

Ex:

res = []
with open(filename, "r") as infile:
    for line in infile:
        if line.strip() == "Hi! How are you?":
            res.append(line.strip())
            lineVal = (next(infile)).strip() 
            if lineVal == "Can you hear me?":
                res.append(lineVal)
                res.append("\n Added new line \n")
        else:
            res.append(line.strip())



with open(filename1, "w") as out_file:
    for line in res:
        out_file.write(line+"\n")

Output:

ricochet robots
settlers of catan
acquire
Hi! How are you?
Can you hear me?

 Added new line 

this is very valuable
finish
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

Is this what you want:

text = "Can you hear me?"
with open("input.txt", "r") as infile:
    readcontent = infile.readlines()

with open("output.txt", "w") as out_file:
    for idx,line in enumerate(readcontent):
       if line.rstrip() == text:
           line+='\nAdded new line\n\n'
       out_file.write(line)

output.txt will look like:

ricochet robots
settlers of catan
acquire
Hi! How are you?
Can you hear me?

Added new line

this is very valuable
finish
U13-Forward
  • 69,221
  • 14
  • 89
  • 114