0

I am trying to add particular segment of txt files multiple times to another txt file

Input.txt

 Heading 
 Heading2 
    
 Heading3 
    
 Line1
 Line2
    
 Line3
 Line4

Outputfile.txt(expected Output)

 Heading 
 Heading2 
    
 Heading3 
    
 Line1
 Line2
 
 Line1
 Line2

 Line1
 Line2
    
 Line3
 Line4

I want to repeat Line1 and Line2 multiple times.

LinesToRepete=[6,7,8]
with open("Input.txt") as f:
    with open("output.txt", "a+") as f1:
        for k in range(2):
            for i, line in enumerate(f):
                if i in LinesToRepete:
                        f1.write(line)
                else:
                    f1.write(line)
Sreevathsabr
  • 649
  • 7
  • 23
  • 1
    The tags suggest you need a solution that works in Python 2 as well as Python 3? Perhaps make that explicit in the question. Also, what is `TestcaseLine` - you never defined it? – Grismar Nov 19 '20 at 04:17
  • Any one of it will also do ..Removing the tag – Sreevathsabr Nov 19 '20 at 04:17
  • 1
    It appears that you don't just want to repeat lines 6 and 7, but also line 8 (empty)? What other values should work for `lines_to_repeat`? Is it always just the first and last line of the section to repeat? Or should it be able to repeat multiple sections? – Grismar Nov 19 '20 at 04:19
  • sorry I copy pasted my code form main code. so typo.Yes i need to repete 6,7,8 lines. Only these lines must repete , Not other part of text file – Sreevathsabr Nov 19 '20 at 04:21
  • Please describe how you identify the lines to be repeated. By position (because of the `enumerate`) or by content? BTW: `with` supports creating multiple contexts. And I suggest to invert the loops – Pynchia Nov 19 '20 at 04:23
  • Anything is fine. I can identify 6,7 line by content, But not line 8 as there are many blank lines. So looking for best-suited one. where i can provide inputs to number of times to repete – Sreevathsabr Nov 19 '20 at 04:25

1 Answers1

1

Your code has multiple issues;

  • When you read a line from a file handle, you consume it; it cannot be read again (unless you close and reopen the file handle, or rewind it). See e.g. Why can't I call read() twice on an open file?
  • Your code would repeat the lines next to each other. If you managed to sort out the previous bug, you would get repeated Line 1 followed by repeated Line 2 etc. By your example, you want to repeat the entire block k times, in the same sequence.
  • enumerate starts its numbering at 0; you can switch to 1-based indexing (or generally any base number) by passing an additional parameter.

Here's an attempt to rectify these bugs. For simplicity, this is just reading standard input and writing to standard output. (This is actually a useful convention because this way, you can use it on any file, without needing to hard-code anything or parse the command line.)

import sys

lines_to_repeat = [5, 6, 7]

memory = []
for i, line in enumerate(sys.stdin, 1):
    if i in lines_to_repeat:
        memory.append(line)
    elif memory:
        sys.stdout.write(''.join(memory))
        memory = []
    sys.stdout.write(line)

If you want to repeat more than once, you can add a loop in the elif memory: block, or just multiply the joined string with the number of additional repeats (in Python, 3 * "abc" evaluates to "abcabcabc").

Demo: https://ideone.com/LGOBHX

An organizing assumption is that you want a single block of contiguous lines to be repeated; if you want to repeat multiple disparate blocks, that will take some rather heavy refactoring. (Probably ask a new question with your real requirements then.)

tripleee
  • 175,061
  • 34
  • 275
  • 318