-1

I have a text file ,i need to get the index value of the first 'Number' found and insert the no.s

Text file:

!Hello World!
   the following no.s
     Number 1
     Number 2
     Number 3

I need to insert a set of no.s after the first no. that is Number 1

code consisting of a set of no.s

f.write( '\n'.join(group.text) + '\n')

Expected output!

!Hello World!
   the following no.s
     Number 1
        12345
        12342
        234567   
     Number 2
     Number 3     

1 Answers1

0

This will add the lines, the padding you want you should be able to figure out

text = ["123456","12342","234567"]
with open("indput.txt") as f,open("output.txt","w") as out:
    lines = f.readlines()
    print lines
    for ind,line in enumerate(lines):
        if line.strip().startswith("Number"):
            lines[ind] = (line+"     "+'\n     '.join(text) + '\n')
            break
    for line in lines:
        out.write(line)
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321