Answer the question
In order to leave comments, you need to log in
How to find all numbers in html file (table) and subtract 20% from each?
<tr>
<td>Установка смесителя в ванной</td>
<td>от 450 рублей</td>
</tr>
<tr>
<td>Установка смесителя на мойку, умывальник</td>
<td>от 450 рублей</td>
</tr>
html_file = open('table.html', 'w')
Answer the question
In order to leave comments, you need to log in
If there are numbers in html tags and comments, they will also change. So if you have numbers there you need to change the regular expression.
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)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question