[Tutor] Modifying files in a directory.
Martin Walsh
mwalsh at groktech.org
Fri May 30 04:06:26 CEST 2008
qsqgeekyogdty at tiscali.co.uk wrote:
>.>> soup = BeautifulSoup(html)
>.>> ord_tbl_price = soup.find('td', {'class': 'order_tbl_price'})
>.>> ord_tbl_price
> <td class="order_tbl_price"><span class="order_table_price_small"
> </span> $32.66</td>
>
> So now, how do I reduce the price by 15% and write it back to the
> document?
Not sure if this is the right way, but it seems to work for me, YMMV:
html = """\
<td class="order_tbl_price"><span
class="order_table_price_small">From</span> $32.66</td>"""
soup = BeautifulSoup(html)
otp = soup.find('td', {'class': 'order_tbl_price'})
price = float(otp.contents[1].lstrip(' $'))
otp.contents[1].replaceWith('$%0.2f' % (price * 0.85))
print soup.renderContents()
"""
<td class="order_tbl_price"><span
class="order_table_price_small">From</span>$27.76</td>
"""
I'll second Kent's suggestion to experiment on the command line, and go
one further -- download and install the ipython interpreter[1]. It
offers tab-completion of methods and attributes of an object (for
example, type yourobject.<tab>), easy access to doc-strings with a
single ?, the source code (when available) with ??, and much much
more[2]. It's a real time saver, even if you don't use the fancier
features it beats dir()/help() hands down.
[1] http://ipython.scipy.org/moin/
[2] http://ipython.scipy.org/doc/manual/node4.html
HTH,
Marty
More information about the Tutor
mailing list