Stripping whitespace

John Machin sjmachin at lexicon.net
Wed Jan 23 23:04:26 CET 2008


On Jan 24, 7:23 am, ryan k <r... at ryankaskel.com> wrote:
> On Jan 23, 3:02 pm, John Machin <sjmac... at lexicon.net> wrote:
>
> > On Jan 24, 6:57 am, ryan k <r... at ryankaskel.com> wrote:
>
> > > So yea i will just have to count dashes.
>
> > Read my lips: *you* counting dashes is dumb. Writing your code so that
> > *code* is counting dashes each time it opens the file is smart.
>
> Okay it's almost working ...
>
> new parser function:
>
> def _load_table(self):
>         counter = 0
>         for line in self.table_fd:
>             # Skip the second line

The above comment is a nonsense.

>             if counter == 0:
>                 # This line contains the columns, parse it
>                 column_list = line.split()

In generality, you would have to allow for the headings to contain
spaces as well -- this means *saving* a reference to the heading line
and splitting it *after* you've processed the line with the dashes.

>             elif counter == 1:
>                 # These are the dashes
>                 line_l = line.split()
>                 column_width = [len(i) for i in line_l]

Whoops.
column_width = [len(i) + 1 for i in line_l]

>                 print column_width
>             else:
>                 # This is a row, parse it
>                 marker = 0
>                 row_vals = []
>                 for col in column_width:
>                     start = sum(column_width[:marker])
>                     finish = sum(column_width[:marker+1])
>                     print line[start:finish].strip()

If you had printed just line[start:finish], it would have been obvious
what the problem was. See below for an even better suggestion.

>                     row_vals.append(line[start:finish].strip())
>                     marker += 1

Using sum is a tad ugly. Here's an alternative:

row_vals = []
start = 0
for width in column_width:
    finish = start + width
    #DEBUG# print repr(line[start:finish].replace(' ', '~'))
    row_vals.append(line[start:finish].strip())
    start = finish

>                 self.rows.append(Row(column_list, row_vals))
>             counter += 1
>
> Something obvious you can see wrong with my start finish code?

See above.




More information about the Python-list mailing list