splitting tables
Scott David Daniels
Scott.Daniels at Acm.Org
Sat Feb 7 16:04:05 EST 2004
robsom wrote:
> ... The 'split' function works pretty well, except when there is an error
> in the original data table. For example if an element is missin in a line,
> like this:
>
> CGA 1990 08 15 13 16 G500-105 D 524.45 J.. R1 1993 01 29 00 00 900069
> CGA 1990 08 16 01 22 D 508.06 J.. R1 1993 01 27 00 00 900065
A way to stay reasonably fast is to use split when it works:
lengths = 3,4,2,2,2,8,1,8,3,2,4,2,2,2,2,6
...
result = line.split()
if len(result) != len(lengths):
result, pos = [], 0
for length in lengths:
next = pos + length + 1
result.append(line[pos : next].strip())
pos = next
...
--
-Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list