python获取索引值并写入文本

时间:2014-08-26 14:32:57

标签: python

我有一个文本文件,我需要获取第一个'数字'的索引值。找到并插入no.s

文字档案:

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

我需要在第一个之后插入一组no.s。那是Number 1

代码由一组no.s

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

预期输出!

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

1 个答案:

答案 0 :(得分:0)

这将添加行,你希望你能想出的填充

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)