On Aug 15, 7:12 am, John Machin <[EMAIL PROTECTED]> wrote:
> On Aug 15, 4:55 am, [EMAIL PROTECTED] wrote:
>
> > #your thought is right.
> > =======================================================
> > def sizes2fields(sizes):
> >    d = []
> >    begin = 0
> >    for i in sizes:
> >       if begin:
> >          end = begin + i
> >       else: end = i
> >       d.append((begin, end))
> >       begin += i
> >    return tuple(d)
>
> Those who are not paid by the keystroke and/or prefer to expend
> keystrokes on meaningful names might like an alternative like this:
> def sizes2offsets(sizes):
>   offsets = []
>   begin = 0
>   for size in sizes:
>     end = begin + size
>     offsets.append((begin, end))
>     begin = end
>   return offsets

This is even shorter: (and IMHO, clearer)

def split(s, fields):
    ret = []
    for field in fields:
        s, d = s[field:], s[:field]
        ret.append(d)
    return ret

sizes = [16, 4, 8, 8, 8]
s = '123456789012345678901234567890123456789012345678901234567890'
print split(s, sizes)

alternatively, if what you've got is the partition position instead of
field width:

def split(s, sizes):
    ret = []
    start = sizes[0]
    for end in sizes[1:]:
        r, start = s[start: end], end
        ret.append(r)
    return ret

sizes = [0, 16, 20, 28, 36, 44]
s = '123456789012345678901234567890123456789012345678901234567890'
print split(s, sizes)

Michael Stroder:
> If the input data has to be pre-processed before storing it into the
> database a Python script would be needed.

But not for converting the fixed length string, you could just have
SQL process the fixed length string then retrieve it back as separate
fields.
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to