import re
# Если скидку сделать отрицательной, то будет наценка 20% :)
discount = 0.2
def repl(match):
return str(int(float(match.group(0)) * (1 - discount)))
with open('table.html', 'r+') as fd:
new_lines = [re.sub(r'(\d+)', repl, line) for line in fd]
fd.seek(0)
fd.writelines(new_lines)
from lxml import etree
html = """
<table id="journal" class="marks">
<tbody>
<tr>
<td class="s2">
<strong class="u">HTML</strong>
</td>
<td class="tac">
<span class="mark mY analytics-app-popup-mark" data-num="0">1</span>
<span class="mark mY analytics-app-popup-mark" data-num="0">2</span>
<span class="mark mG analytics-app-popup-mark" data-num="0">3</span>
</td>
</tr>
</tbody>
</table>"""
tree = etree.fromstring(html)
for block in tree.xpath("//table[@id='journal']/descendant::span[starts-with(@class, 'mark')]"):
print(block.xpath("text()"))
with open('tmp', 'w') as file:
file.write('line 1\n')
with open('tmp', 'a') as file:
file.write('line 2\n')
with open('tmp', 'r+') as fd:
contents = fd.readlines()
contents.insert(1, 'line 3\n')
fd.seek(0)
fd.writelines(contents)
with open('tmp', 'r+') as fd:
contents = fd.readlines()
contents.insert(2, 'line 4\n')
fd.seek(0)
fd.writelines(contents)
with open('tmp') as f:
for line in f:
print(line, end="")