how to avoid leading white spaces

Ian Kelly ian.g.kelly at gmail.com
Mon Jun 6 13:40:51 EDT 2011


On Mon, Jun 6, 2011 at 11:17 AM, Neil Cerutti <neilc at norwich.edu> wrote:
> I wrestled with using addition like that, and decided against it.
> The 7 is a magic number and repeats/hides information. I wanted
> something like:
>
>   prefix = "TABLE='"
>   start = line.index(prefix) + len(prefix)
>
> But decided I searching for the opening ' was a bit better.

Fair enough, although if you ask me the + 1 is just as magical as the
+ 7 (it's still the length of the string that you're searching for).
Also, re-finding the opening ' still repeats information.

The main thing I wanted to fix was that the second .index() call had
the possibility of raising an unhandled ValueError.  There are really
two things we have to search for in the line, either of which could be
missing, and catching them both with the same except: clause feels
better to me than checking both of them for -1.

Cheers,
Ian



More information about the Python-list mailing list