I'm working on a Python script that when I run it will cobble together text from different files so that I can create alternative versions of a website to easily compare different designs and also make sure that they all have the same inherent data, viz. that the menu items are consistent across all versions.
One specific problem area is making sure that the menu, which is cornucopia of different meals, is always the same. Therefore I've made this function:
def insert_menu():
with open("index.html", "r+") as index:
with open("menu.html", "r+") as menu:
for i in index:
if "<!-- Insert Menu here -->" in i:
for j in menu:
index.write(j)
However, it doesn't behave the way I want it to because I have been unable to find a way to extract what I need from other answers here on Stack Overflow.
In it's current state it will append the text that I have stored in menu.html
at the end of index.html
.
I want it to write the text in menu.html
below the line, which is in index.html
(but not necessarily always at the same line number, therefore ruling out the option of writing at a specific line) , containing <!-- Insert Menu here -->
. Then, after everything inside of menu.html
has been written to index.html
I want for index.html
to "continue" so to speak.
Basically I mean to wedge the text from menu.html
into index.html
after the line containing <!-- Insert Menu here -->
but there is more text underneath that comment that I must retain (scripts and et al.)
Copied from the index.html
document, this is what 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 that in index.html
the above block is all indented inside a larger div, and I cannot easily replicate this here on SO.
How can I change my code to achieve the desired result, or am I going about this in a very roundabout way?
And, how can I clarify this question to help you help me?