Possible to slice a string with unpacked tuple?

python at bdurham.com python at bdurham.com
Sat Jan 24 20:56:15 EST 2009


Terry,

Hi, I'm the OP in question.

> the OP should simply write myString[slice(*myTuple)]

My tuples define positions in a fixed width string for parsing values.
So rather than 'unpacking' tuples, I'm using slice() to directly define
actual field positions, eg.

field_1 = slice( 4, 7 )
field_2 = slice( 7, 9 )
etc ...

This allows me to extract the field values as follows:

for line in myOpenFile:
    value_1 = line[ field_1 ]
    value_2 = line[ field_2 ]
    etc.

IMO, this makes a common task like extracting fields very natural and
elegant.

I LOVE THIS LANGUAGE!! (to echo another recent thread in this forum)

Regards,
Malcolm



More information about the Python-list mailing list