Insert text into a text file following specific text using Python

前端 未结 3 1542
没有蜡笔的小新
没有蜡笔的小新 2021-02-10 16:33

I have to edit some text files to include new information, but I will need to insert that information at specific locations in the file based on the surrounding text.

Th

3条回答
  •  被撕碎了的回忆
    2021-02-10 16:56

    You could use a regex and then replace the text.

    import re
    c = "This is a file's contents, apparently you want to insert text"
    re.sub('text', 'text here', c)
    print c
    

    returns "This is a file's contents, apparently you want to insert text here"

    Not sure if it'll work for your usecase but it's nice and simple if it fits.

提交回复
热议问题