1

I have an hmtl file that looks like:

...
<!-- Special_ID -->
<p> stuff1 </p>
<p> stuff2 </p>
<!-- /Special_ID -->
...

I have an INI file:

[general]
param=stuff1
 stuff2

If the user edits the file and changes the param value to test, I want the html file to be changed to:

...
<!-- Special_ID -->
<p> test </p>
<!-- /Special_ID -->
...

Currently, what I'm doing is parsing the INI file (Python's ConfigParser) and then turning the section ("general") and option ("param") into a start and stop special id like in the examples above.

Then:

while we haven't found the start id:
    just write a line to some temporary file

write our start id to the temp file
write out new value ("test") to the temp file # surround with <p>

loop through original file until we find the stop id
then write the stop id and the rest of the file to temp

replace original file with tmp file

Is there a smarter way of doing this?

Perhaps a Python module that already does this.

I also don't particularly like requiring the <!-- Special_ID -->, but I'm not using a web framework (just a simple app), so I can't just do a fancy <p py:for ...>... like in TurboGears.

1 Answer 1

1

Overall not sure about the current approach you've presented, but here is how you can replace all the p elements after a specific comment and insert a new p element instead (using BeautifulSoup HTML parser). The idea is to:

The working code:

from bs4 import BeautifulSoup, Comment

data = """
<!-- Special_ID -->
<p> stuff1 </p>
<p> stuff2 </p>
<!-- /Special_ID -->
"""
soup = BeautifulSoup(data, "html.parser")

# find "Special_ID" comment
special_id = soup.find(text=lambda text: isinstance(text, Comment) and "Special_ID" in text)

# find all sibling "p" elements
for p in special_id.find_next_siblings("p"):
    p.extract()

# create new "p" element
tag = soup.new_tag("p")
tag.string = "test"

# insert the new "p" element after the comment
special_id.insert_after(tag)

print(soup.prettify())

Prints:

<!-- Special_ID -->
<p>
 test
</p>
<!-- /Special_ID -->
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, this is a great idea! Also, you implied a good point. If I assume that I will only ever need to replace the sibling p tags, then I don't need a special id comment to close. Instead of removing up to the close comment, remove up through the last p tag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.