Stripping whitespace

John Machin sjmachin at lexicon.net
Wed Jan 23 14:53:09 EST 2008


On Jan 24, 6:17 am, ryan k <r... at ryankaskel.com> wrote:
> I am taking a database class so I'm not asking for specific answers.
> Well I have this text tile:
>
> http://www.cs.tufts.edu/comp/115/projects/proj0/customer.txt

Uh-huh, "column-aligned" output.

>
> And this code:
>
[snip]

>
> Because the addresses contain spaces, this won't work because there
> are too many values being unpacked in row's __init__'s for loop. Any
> suggestions for a better way to parse this file?


Tedious (and dumb) way:
field0 = line[start0:end0+1].rstrip()
field1 = line[start1:end1+1].rstrip()
etc

Why dumb: if the column sizes change, you have to suffer the tedium
again. While your sample appears to pad out each field to some
predetermined width, some reporting software (e.g. the Query Analyzer
that comes with MS SQL Server) will tailor the widths to the maximum
size actually observed in the data in each run of the report ... so
you write your program based on some tiny test output and next day you
run it for real and there's a customer whose name is Marmaduke
Rubberduckovitch-Featherstonehaugh or somesuch and your name is mud.

Smart way: note that the second line (the one with all the dashes)
gives you all the information you need to build lists of start and end
positions.



More information about the Python-list mailing list