html - Python: Write an entire file to another file after a specific line that meets a particular condition -


i'm working on python script when run cobble text different files can create alternative versions of website compare different designs , make sure have same inherent data, viz. menu items consistent across versions.

one specific problem area making sure menu, cornucopia of different meals, same. therefore i've made function:

def insert_menu():     open("index.html", "r+") index:         open("menu.html", "r+") menu:             in index:                 if "<!-- insert menu here -->" in i:                     j in menu:                         index.write(j) 

however, doesn't behave way want because have been unable find way extract need other answers here on stack overflow.

in it's current state append text have stored in menu.html @ end of index.html.

i want write text in menu.html below line, in index.html (but not @ same line number, therefore ruling out option of writing @ specific line) , containing <!-- insert menu here -->. then, after inside of menu.html has been written index.html want index.html "continue" speak.

basically mean wedge text menu.html index.html after line containing <!-- insert menu here --> there more text underneath comment must retain (scripts , et al.)

copied index.html document, surrounds <!-- insert menu here -->:

<html>     <body>         <div id="wrapper">             <div id="container">                  <div class="map">                 </div>                  <!-- insert menu here -->              </div><!-- container ends here -->         </div><!-- wrapper ends here -->     </body> </html> 

note in index.html above block indented inside larger div, , cannot replicate here on so.

how can change code achieve desired result, or going in roundabout way?

and, how can clarify question me?

assuming files aren't huge, simple string replacement work:

def insert_menu():     open("index.html") index:         index_text = index.read()     open("menu.html") menu:         menu_text = menu.read()     # called index2 doesn't overwrite... can change     open("index2.html", "w") index2:         index2.write(index_text.replace('<!-- insert menu here -->', menu_text)) 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -