Is there a way to scan an entire text document and say find everything that says “lol” and replace it with the id value of the first previous chapter tag? maybe something like this.
python
x=open('source.txt')
lines = x.readlines()
for line in lines:
if line.startswith('<text'):
line.replace('lol', first previous chapter id value)
x.write(lines)
x.close()
source text
<chapter id="1">
<text class="lol">
<text class="lol">
<chapter id="2">
<text class="lol">
<text class="lol">
<chapter id="3">
<text class="lol">
<text class="lol">
<chapter id="4">
<text class="lol">
<text class="lol">
result text
<chapter id="1">
<text class="1">
<text class="1">
<chapter id="2">
<text class="2">
<text class="2">
<chapter id="3">
<text class="3">
<text class="3">
<chapter id="4">
<text class="4">
<text class="4">
Try that. Basically all you needed to do extra was find that chapter id. Also I’m assuming you know to write to file, hence why I just printed each line.
Output: