[Tutor] Modifying files in a directory.

Kent Johnson kent37 at tds.net
Fri May 30 14:43:35 CEST 2008


On Fri, May 30, 2008 at 7:52 AM, qsqgeekyogdty at tiscali.co.uk
<qsqgeekyogdty at tiscali.co.uk> wrote:
> Yes I do need to learn the basics of programming ;)
>
> This was the first I did, was to put the modification code into the
> loop, but had this error:
>
>>>> for x in otp:
> ...     price = float(x.contents[1].lstrip(' $'))
> ...     x.contents[1].replaceWith('$%0.2f' % (price * 0.85))
> ...
> Traceback (most recent call last):
>  File "<stdin>", line 2, in <module>
> IndexError: list index out of range

Your <td> cells do not have the same structure, some have no <span>,
some have a <span> before the price, the price may be inside a <span>
or not...you will have to write your program to deal with this
variation.

I guess your loop failed on the first <td> that doesn't have a <span>.
If you print x in the loop that would help you figure out which one is
the problem.

Kent


More information about the Tutor mailing list